Mail Archives: djgpp/1997/09/03/07:28:22
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 -