CPP Strings
CPP Strings
#include <string> using namespace std; OR #include <string> using std::string; if (s1 == s2) cin >> s1; getline(cin, s1); // are they equal? // Read chars from keyboard into s1 until white space is encountered. // Read chars from keyboard into s1 until \n is encountered. string s1 = hello; string s2 = s1; s1 = s1 + there; // s2 is hello // s1 is hello there
Prototype
bool int char empty() length() at(int pos)
Example
string a = test; if (a.empty()); // false string a = hello; int len = a.length(); // 5 string a = hello; char c = a.at(0); // same as c = a[0] a.at(0) = H; // a is Hello string a = hello; char *p = a.c_str(); // hello string a = test; if (a.compare(test) == 0) // same string a = compare test; if (a.compare(2, 3, yes test) == 0) // compare mpa to yes string a = erase this; a.erase(0, 6); // Leaves this a.erase(2); // Leaves th string a = again; a.append(state, 0, 2); // against string a = to; a.resize(4); a[2] = b; a[3] = e; // a = tobe string a = a nice play; string b = a.substr(2, 4); // nice string a = this is a test; int pos = a.find(is); // returns 2 pos = a.find(is, 3); // returns 5 pos = a.rfind(is); // returns 5 string a = this is a test; pos = a.find_first_of(iou); // 2 pos = a.find_last_of(iou); // 5
Description
Returns true if nothing is in the string, false otherwise. Returns the number of chars in the string. Same as size(). Returns the char at position pos. Same functionality as using [pos] notation. Returns a char array representing the string with a null terminator at the end. Compares the string to s, returns 0 if the string and s are equal, < 0 if the string is before s, and > 0 if after s. Just like < > ==. Compares numChars chars starting at position start to string s. Note: g++ compiler requires string to be first arg, not last. Erases all chars to the right of the starting position start. Erases numChars number of chars beginning at position start. Appends numChars chars from s to the string, beginning at slot start in s. Resizes the string so it can hold additional characters.
char * int
c_str()
compare(string s)
int compare(int start, int numChars, string s) erase(int start) erase(int start, int numChars) append(string s, int start, int numChars) resize(int size)
string int int int int int int int int int int
string a = this is a test; Returns first (or last) occurrence of any char not in s or pos = a.find_first_not_of(ihst); // 4 string::npos if at least one of the chars in s could be found in pos = a.find_last_not_of(ihst); // 11 every position of the string. replace(int start, int numChars, string a = hello there; Replaces numChars chars starting at position start with s. string s) a.replace(0, 5, stay); // stay there insert(int start, string s) a = this is a test; Inserts s at position start in the string. a.insert(8, not ); // this is not a test swap(string s) string a = one, b = two; Swaps string s with the string. a.swap(b); // a = two, b = one * Note: There are many variations of the functions above which are not displayed here. // Prints all items in a string that are separated by a common delimiter. void parse(string parseString, string delimiter) { string value; int startPos = 0, pos = parseString.find(delimiter); while (pos != string::npos) { value = parseString.substr(startPos, pos - startPos); cout << value << endl; startPos = pos + delimiter.length(); pos = parseString.find(delimiter, startPos); } value = parseString.substr(startPos, parseString.length() - startPos); cout << value << endl; } // Example call: parse("this::is::a::test", "::");
substr(int start, int numChars) find(string s) find(string s, int start) rfind(string s) rfind(string s, int start) find_first_of(string s) find_first_of(string s, int start) find_last_of(string s) find_last_of(string s, int start) find_first_not_of(string s) find_last_not_of(string s)
Return a substring starting at slot start with numChars chars. Returns the position of the first occurrence of s (starting at position 0 or start) or string::npos if the string is not found. Reverse find starts searching at back of string. Returns the first (or last) occurrence of any char in s or string::npos if none of the chars could be found. The parameter start can be used to start the search at that position.