C-style strings





C-style string functions

Some functions to handle and manipulate C-style strings:





C++ String Class

#include <string> string name1 ; // empty string string name2 ("Bob"); // initialize to "Bob" string name3 = "Jones"; // another way to init string name4(name3) ; // yet another "Bob" name1 = name2 ; // makes copy char c2 ; c2 = name1[i] ; // i-th character name1 == name2 ; // string comparison name1 < name2 ; name1 = name2 + name3 ; // concatenation



Some String Methods

string str("Hello World") ; unsigned int n ; n = str.size() ; // returns number of chars in str if ( !str.empty() ) { // if not empty ... } printf("%s", str.c_str() ) ; // returns C-style string cout << str << endl ; // prints "Hello World\n" ; cout << str.substr(2,5) ; // prints "llo W"



C++ String I/O