Method prototype/operator | Explanation | Example |
---|---|---|
string() | default constructor; creates an empty string | string str1; |
void assign(string str) | this string becomes str | str1.assign(str2) |
= | same as assign | str1 = str2 |
>> | extraction operator; ignores leading whitespace and extracts and stores characters until whitespace is found | cin >> str1 |
<< | insertion operator | cout << str1 |
void getline(istream& iStr, string& str) | extracts and stores all characters, including whitespace, from the stream,iStr, through the next new line (\n) character. The newline character is extracted but not stored. Note that this is not a member function so the dot(.) operator is not used. | getline(cin, str1) |
void getline(istream& iStr, string& str, char delimiter) | extracts and stores all characters, including whitespace, from the stream,iStr, through the next delimiter. The delimiter is extracted but not stored. Note that this is not a member function so the dot(.) operator is not used. | getline(cin,str1,':') |
== | equality operator | str1 == str2 |
!= | inequality operator | str1 != str2 |
< | less than operator | str1 < str2 |
<= | less than or equal operator | str1 <= str2 |
> | greater than operator | str1 > str2 |
>= | greater than or equal operator | str1 >= str2 |
int length() | returns the number of characters in this string | i = str1.length() |
char at(int pos) | returns the character at position pos. Remember that strings start at position 0! | c = str1.at(5) |
int find(string str) | returns the position of the first occurrence of str in this string. Returns string::npos if str is not found. | i = str1.find(str2) |
void insert(int start, string str) | insert str into this string starting at position start | str1.insert(5,str2) |
void erase(int start, int n) | erase the next n characters from this string starting at position start | str1.erase(5,8) |
void replace(int start, int n, string str) | replace the next n characters, starting at position start, with str. | str1.replace(5,6,str2) |
void assign(string str, int start, int n) | this string becomes the substring of str, starting at position start and n characters long. | str1.assign(str2,5,6) |