| Method prototype/operator | Explanation | Example |
|---|---|---|
| string() | CS1620 | 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,'*'); |
| = | CS1620 | str1 = str2; |
| = | CS1620 | 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) | CS1620 | str1.assign(str2,5,6); |
| string& assign(char []) | assign an array to this string | str1.assign("help"); |
| >> | CS1620 | cin >> str1; |
| << | CS1620 | cout << str1; |
void getline(istream& iStr,
string& str) | CS1620 | getline(cin, str1); |
void getline(istream& iStr,
string& str,
char delimiter) | CS1620 | getline(cin,str1,':'); |
| int length() | CS1620 | 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) | CS1620 | c = str1.at(5); str1.at(5) = 't'; |
| [ ] | CS1620 | c = str1[5]; str1[5] = 't'; |
| == | CS1620 | if (str1 == str2) ... |
| != | CS1620 | if (str1 != str2) ... |
| < | CS1620 | if (str1 < str2) ... |
| <= | CS1620 | if (str1 <= str2) ... |
| > | CS1620 | if (str1 > str2) ... |
| >= | CS1620 | 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'); |
| + | CS1620 | str1 + str2; str1 + "tion"; "con" + str1; str1 + 'h'; 'u' + str1; |