delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/09/03/07:28:22

From: eyal DOT ben-david AT aks DOT com
To: weiqigao AT a DOT crl DOT com
cc: djgpp AT delorie DOT com
Message-ID: <42256507.00434228.00@aks.com>
Date: Wed, 3 Sep 1997 14:23:15 +0200
Subject: Re: Help with strings MID$?
Mime-Version: 1.0



Weiqi Gao  Wrote:

>One way of doing it is to use the GNU C++ class library libgpp.a's
>String class.  The String class has a member function called at().  Your
>BASIC code could be translated to:
>
>You might also want to check out the third edition of The C++ Language,
>which describes the standard C++ library.  (I haven't checked, but) I
>imagine there would be an ANSI C++ String class that has a member
>function that does exactly what the String::at() function does.


Here is a sample C++ program that uses the standard C++ string class.
The function you are looking for is   string::substr(...).
it takes two parameters the first is location in the string, the other is
the
length of the substring that you want to extract.

#include <iostream.h>
#include <string>


int main()
{
   string s1 = "Hello DJGPP";

   // first parameter is the location, second is LENGTH.
   //
   cout << s1.substr(0, 5) << "\n";

   // treat 'string::npos' as infinity
   //
   int pos = s1.find('D');
   cout << s1.substr(pos, string::npos) << "\n";

   return 0;
}



- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019