<<< std::map | Index | >>> |
A string is an STL container designed to accommodate sequence of characters, that is, text.
Strings provide built-in features to operate on fragments of text and individual characters.
The design is more intuitive than zero-terminated character arrays, commonly known as C-strings.
#include <cassert> #include <string> using namespace std; int main (int argc, char* argv[]) { string str( "Hello" ); for ( int idx = 0; idx < str.length(); ++idx ) { // Access and modify individual characters: str[ idx ] = toupper( str[ idx ] ); } assert( str == "HELLO" ); str.append( 3, '!' ); // add three exclamation points assert( str == "HELLO!!!" ); int pos1 = str.find( "L" ); // HELLO!!! int pos2 = str.rfind( "L" ); // || assert( pos1 == 2 ); // pos1-''-pos2 assert( pos2 == 3 ); // Get 2-character long substring starting at pos1: assert( str.substr( pos1, 2 ) == "LL" ); str.replace( pos1, 3, "YDAY" ); // HE...!!! assert( str == "HEYDAY!!!" ); // '-----pos1 return 0; }
For more information, see CIS-62 std::string coverage.
<<< std::map | Index | >>> |