| Method prototype/operator | Explanation | Example |
|---|---|---|
| string() | default constructor; creates an empty string | string str1; |
string(const string& str,
int start = 0;
int n = npos) | 1 parameter - copy constructor
2 parameters - copies from start to end of str 3 parameters - copies n characters from str starting at start | string str1(str2); string str1(str2,5); string str1(str2,5,20); |
| string(char[]) | conversion constructor - converts an array to a string | string str1("Hello"); |
| string(int howMany, char c) | create a string of howMany characters which are all c | string str1(80,'*'); |
| = | assign a string to this string | str1 = str2; |
| = | assign a character array to this string | str1 = "help"; |
| = | assign a single character to this string | str1 = 'h'; |
| string& assign(const string& str) | assign str to this string. | str1.assign(str2); |
string& assign(const string& str,
int start, int n) | assign n characters from str beginning at start to this string | str1.assign(str2,5,6); |
| string& assign(char []) | assign an array to this string | str1.assign("help"); |
| >> | 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 newline (\n) character. The newline character is extracted but not stored. If a newline character is found, the extracted characters are stored in str. If not, i.e. iStr.eof() is true, then str is not changed. 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. If a delimiter character is found, the extracted characters are stored in str. If not, i.e. iStr.eof() is true, then str is not changed. Note that this is not a member function so the dot(.) operator is not used. | getline(cin,str1,':'); |
| int length() | returns the number of characters in this string | i = str1.length(); |
| int size() | same as length. | i = str1.size(); |
| int capacity() | the capacity of this string | i = str1.capacity(); |
| int max_size() | the longest that this string can be | i = str1.max_size(); |
| bool empty() | returns true if this string has no characters in it | if (str1.empty()) ... |
| void resize(int n) | change the size to n. | str1.resize(20); |
| void swap(string& str) | swaps the contents of the 2 strings | str1.swap(str2); |
| char& at(int pos) | returns the character at position pos. Remember that strings start at position 0! If pos is not a valid position, the program will Abort at runtime. This is an l-value, that is, it can be used on the left hand side of an assignment statement. | c = str1.at(5); str1.at(5) = 't'; |
| [ ] | same as at except that the index is not checked. | c = str1[5]; str1[5] = 't'; |
| == | equality operator | if (str1 == str2) ... |
| != | inequality operator | if (str1 != str2) ... |
| < | less than operator | if (str1 < str2) ... |
| <= | less than or equal operator | if (str1 <= str2) ... |
| > | greater than operator | if (str1 > str2) ... |
| >= | greater than or equal operator | if (str1 >= str2) ... |
| int compare(const string& str) const | three-way lexicographical comparison of str with this string. It returns : < 0 if this string comes before str; = 0 if this string is the same as str; > 0 if this string comes after str | i=str1.compare(str2); |
int compare(int start, int length,
const string& str) const | three-way lexicographical comparison of str with a substring of this string. | i=str1.compare(5, 10, str2); |
int compare(int start, int length,
const string& str,
int sStart,
int sLength) const | three-way lexicographical comparison of a substring of str with a substring of this string. | i=str1.compare(5, 10, str2, 6, 10); |
int find(const string& str,
int start = 0) const | returns the position of the first occurrence of str in this string starting at start. Note: all versions of find functions return string::npos if str is not found. | i=str1.find(str2); i=str1.find(str2,7); |
int find(const char[] ca,
int start = 0) const | returns the position of the first occurrence of the array ca in this string starting at start. | i=str1.find("abc"); i=str1.find("abc",7); |
int find(char c,
int start = 0) const | returns the position of the first occurrence of the character c in this string starting at start. | i=str1.find('X'); i=str1.find('X',7); |
int rfind(const string& str,
int start = npos) const | returns the position of the last occurrence of str in this string starting at start. | i=str1.rfind(str2); i=str1.rfind(str2,45); |
int rfind(const char[] ca,
int start = npos) const | returns the position of the last occurrence of the array ca in this string starting at start. | i=str1.find("abc"); i=str1.find("abc",7); |
int rfind(char c,
int start = npos) const | returns the position of the last occurrence of the character c in this string starting at start. | i=str1.find('X'); i=str1.find('X',7); |
int find_first_of(
const string& str,
int start = 0) const | returns the position of the first occurrence of any character in str found in this string starting at start. | i=str1.find_first_of(str2); i=str1.find_first_of(str2,7); |
int find_first_of(
const char[] ca,
int start = 0) const | returns the position of the first occurrence of any character in ca found in this string starting at start. | i=str1.find_first_of("aeiou"); i=str1.find_first_of("aeiou",7); |
int find_last_of(
const string& str,
int start = npos) const | returns the position of the last occurrence of any character in str found in this string starting at start. | i=str1.find_last_of(str2); i=str1.find_last_of(str2,7); |
int find_last_of(
const char[] ca,
int start = npos) const | returns the position of the last occurrence of any character in ca found in this string starting at start. | i=str1.find_last_of("aeiou"); i=str1.find_last_of("aeiou",7); |
int find_first_not_of(
const string& str,
int start = 0) const | returns the position of the first occurrence of any character not in str found in this string starting at start. | i=str1.find_first_not_of (str2); i=str1.find_first_not_of (str2,7); |
int find_first_not_of(
const char[] ca,
int start = 0) const | returns the position of the first occurrence of any character not in ca found in this string starting at start. | i=str1.find_first_not_of ("aeiou"); i=str1.find_first_not_of ("aeiou",7); |
int find_last_not_of(
const string& str,
int start = npos) const | returns the position of the last occurrence of any character not in str found in this string starting at start. | i=str1.find_last_not_of (str2); i=str1.find_last_not_of (str2,7); |
int find_last_not_of(
const char[] ca,
int start = npos) const | returns the position of the last occurrence of any character not in ca found in this string starting at start. | i=str1.find_last_not_of ("aeiou"); i=str1.find_last_not_of ("aeiou",7); |
string& insert(int start,
const string& str) | insert str into this string starting at position start | str1.insert(5,str2) |
string& insert(int start,
const char[] ca) | insert the array ca into this string starting at position start | str1.insert(5,"help"); |
string& insert(int start,
const string& str,
int sStart, int n) | insert a substring of str into this string starting at position start | str1.insert(5,str2,10,5); |
string& erase(int start = 0,
int n = npos) | erase the next n characters from this string starting at position start | str1.erase(); str1.erase(5); str1.erase(5,8); |
string& replace(int start, int n,
const string& str) | replace the next n characters, starting at position start, with str. | str1.replace(5,6,str2) |
string& replace(int start, int n,
const string& str,
int sStart, int sN) | replace the next n characters, starting at position start, with a substring of str. | str1.replace(5,6,str2,3,9); |
string substr(int start = 0,
int n = npos) const | return a substring of this string. No parameters - the whole string 1 parameter - from start to end of string 2 parameters - from start containing n characters or to end of string. | str1 = str2.substr(); str1 = str2.substr(5); str1 = str2.substr(5,10); |
| string& append(const string& str) | append str to this string. | str1.append(str2); |
| string& append(const char[] ca) | append the array ca to this string. | str1.append("tion"); |
string& append(const string& str,
int start, int n) | append a substring of str to this string. | str1.append(str2,3,9); |
| += | same as append(string) | str1 += str2; |
| += | same as append(char[]) | str1 += "help"; |
| += | append a single character to this string | str1 += 'h'; |
| void push_back(char c) | append a single character to this string | str1.push_back('h'); |
| + | return the concatenated string | str1 + str2; str1 + "tion"; "con" + str1; str1 + 'h'; 'u' + str1; |