# C++ Notes: <string> class

Assume the following declarations:

       string s, s1, s2;
       char c;
       char* cs;
       int i, start, len, start1, len1, start2, len2, newSize;
       istringstream istr; // requires <sstream>
       ostringstream ostr; // requires <sstream>

## Methods and operators

Type | Method | Description
--- | --- | ---
Constructors and destructors
`` | `string s;` | Creates a string variable.
`` | `string s(s1);` | Creates s; initial value from s1.
`` | `string s(cs);` | Creates s; initial value from cs.
Altering
`` | `s1 = s2;` | Assigns s2 to s1.
`` | `s1 = cs;` | Assigns C-string cs to s1.
`` | `s1 = c;` | Assigns char c to s1.
`` | `s[i] = c;` | Sets ith character. Subscripts from zero.
`` | `s.at(i) = c;` | As subscription, but throws `out_of_range` if i isn't in string.
`` | `s.append(s2);` | Concatenates s2 on end of s. Same as s += s2;
`` | `s.append(cs);` | Concatenates cs on end of s. Same as s += cs;
`` | `s.assign(s2, start, len);` | Assigns s2[start..start+len-1] to s.
`` | `s.clear();` | Removes all characters from s
Access
`cs =` | `s.c_str();` | Returns the equivalent c-string.
`s1 =` | `s.substr(start, len);` | s[start..start+len-1].
`c =` | `s[i];` | ith character. Subscripts start at zero.
`c =` | `s.at(i);` | As subscription, but throws `out_of_range` if i isn't in string.
Size
`i =` | `s.length();` | Returns the length of the string.
`i =` | `s.size();` | Same as s.length()
`i =` | `s.capacity();` | Number of characters s can contain without reallocation.
`b =` | `s.empty();` | True if empty, else false.
`i =` | `s.resize(newSize, padChar);` | Changes size to newSize, padding with padChar if necessary.
Searching All searches return `string::npos` on failure.
`i =` | `s.find(c);` | Position of leftmost occurrence of char c.
`i =` | `s.find(s1);` | Position of leftmost occurrence of s1.
`i =` | `s.rfind(s1);` | As find, but right to left.
`i =` | `s.find_first_of(s1);` | Position of first char in s which is in s1 set of chars.
`i =` | `s.find_first_not_of(s1);` | Position of first char of s not in s1 set of chars.
`i =` | `s.find_last_of(s1);` | Position of last char of s in s1 set of chars.
`i =` | `s.find_last_not_of(s1);` | Position of last char of s not in s1 set of chars.
Comparison
`i =` | `s.compare(s1);` | <0 if s<s1, 0 if s==s1, or >0 if s>s1.
`i =` | `s.compare(start1, len1, s1, start2, len2);` | Compares s[start1..start1+len1-1] to s1[start2..start2+len2-1]. Returns value as above.
`b =` | `s1 == s2 also > < >= <= !=` | The comparison operators work as expected.
Input / Output
`` | `cin >> s;` | >> overloaded for string input.
`` | `getline(cin, s);` | Next line (without newline) into s.
`` | `cout << s;` | << overloaded for string output.
`` | `istr.str(s);` | Sets input string stream to `s`.
`s =` | `ostr.str();` | Returns string generated in output string stream.

## Concatenation

The + and += operators are overloaded to concatentate/append two strings.

    s = s1 + s2;
    s += s2;
    s += cs;
    s += c;

## And much more

And lots more: replace, insert, iterators, string streams, different parameter types, ...