String

String manipulation is a very important and practical part of computer science and computer programming. While the contents of a string is usually used to store a message for humans to read, it may also be used to store any kind of data in a format that's compatible with any computer.

A string is essentially a vector of characters, but with additional features that are useful and particular to a vector of characters. For the following expressions, assume s and t are string variables, and i and n are integers.

Expression Description
s = t Assigns t to s
s[i] Accesses the character at index i in s
s.size() Returns the number of characters in s
s = "" Erases all characters in s
s.insert(i, t) Inserts the t into s at index i
s.erase(i, n) Erases n characters at index i from s
s += t Appends t to s
s.substr(i, n) Returns a copy of the substring residing at index i in s and occupying n characters
s.find(t) Returns the index of the first occurrence of substring t in s or returns string::npos if t was not found in s
s.rfind(t) Returns the index of the last occurrence of substring t in s or returns string::npos if t was not found in s
s + t Returns a new string that is the concatenation of s and t
s == t Returns true if s and t contain the same characters in the same order
s != t Returns true if s and t do not contain the same characters in the same order
swap(s, t) Exchanges the contents of s and t
stoi(s) Converts a string to int
stod(s) Converts a string to double
to_string(x) Converts an int or double to string

Let's look at some examples of the operations that require one to specify an index and a count. Assume string s = "Hello world"; has been declared, here is a "graphical" representation of our string:

0 1 2 3 4 5 6 7 8 9 10 +---+---+---+---+---+---+---+---+---+---+---+ | h | e | l | l | o | | w | o | r | l | d | +---+---+---+---+---+---+---+---+---+---+---+

There are 11 characters, each indexed from 0 to 10. So currently, s.size() will return 11, s[0] will return 'h', s[1] will return 'e', ..., and s[10] will return 'd'

The expression s.insert(6, "big ") will modify s by inserting "big " into s at index 6:

+---+---+---+---+ | b | i | g | |---------+ +---+---+---+---+ | V 0 1 2 3 4 5 6 7 8 9 10 +---+---+---+---+---+---+---+---+---+---+---+ | h | e | l | l | o | | w | o | r | l | d | +---+---+---+---+---+---+---+---+---+---+---+ ... 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ | h | e | l | l | o | | b | i | g | | w | o | r | l | d | +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+

The result is that the characters at index 6 will be "big " and the characters that previously occupied that index are shifted over to the right.

The reverse can be accomplished using erase with s.erase(6, 4):

erase(6, 4) +-----------+ | | V V 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ | h | e | l | l | o | | b | i | g | | w | o | r | l | d | +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ ... 0 1 2 3 4 5 6 7 8 9 10 +---+---+---+---+---+---+---+---+---+---+---+ | h | e | l | l | o | | w | o | r | l | d | +---+---+---+---+---+---+---+---+---+---+---+

Erase effectively shifts characters from the right to the left to cover over the substring that one is erasing. With the substring member function, one can extract a portion of this string; s.substr(0, 5) returns a string containing "hello" and s.substr(6, 5) returns a string containing "world".

substr(0, 5) substr(6, 5) +---------------+ +---------------+ | | | | V V V V 0 1 2 3 4 5 6 7 8 9 10 +---+---+---+---+---+---+---+---+---+---+---+ | h | e | l | l | o | | w | o | r | l | d | +---+---+---+---+---+---+---+---+---+---+---+

These operations can be combined, and code can be written to erase all occurrences of a particular word from a string:

string s = "The red car was parked by the red house"; for (int i = s.find("red "); i != string::npos; i = s.find("red ")) s.erase(i, 4); cout << s << endl;

The code above outputs "The car was parked by the house"