C++ Strings

When working with a string of characters, C++ provides a number of methods, along with other functionality, that allows for them to be easily analysed and manipulated. In order to utilise some of this functionality the ‘string’ header file needs to be included.

When working with the console, a string can be displayed using a ‘cout’ statement as mentioned previously.

cout << "Hello world!" << endl;

If it is necessary to incorporate the value of a variable into this message it can be done as follows.

string fname = "Bob";

cout << "Hello " << fname << "!" << endl;

This will display the message, “Hello Bob!”, in the console.


As discussed previously, the plus symbol (+) is an arithmetic operator, which can be used to add two numeric values together, however, it can also be used to concatenate, or join, strings together.

string fname = "Bob";
string message = "Hello " + fname + "!";

cout << message << endl;

Here, the string ‘Hello ‘ is concatenated with the value of the variable ‘fname’, followed by ‘!’, to display the same message as above.

Index Value

Like arrays and vectors, every character in a string has an index value, which can be used to reference it. The first character has an index value of zero, the second has an index of one and so on.

string example = "This is a string.";

cout << example[3] << endl;

Here, the index value of three, in square brackets, is used to output the fourth character of the string to the console.

It is possible to find the index position of a character or string within another string using the ‘find’ method. It should be noted that, although an index position appears to be an integer, which can be stored in a variable of type ‘int’, it actually requires a variable of type ‘size_t’.

string example = "This is a string.";
string searchString = "is";
size_t position = example.find(searchString);

if (position != string::npos)
    cout << "String found at index position: " << position << endl;
    cout << "String not found." << endl;

Where a string is being searched for, the position returned is that of the first character. The reason for the ‘if’ statement here is to handle if the string being searched for is not found. If the string is not found then the greatest possible value for a variable of type ‘size_t’ is returned, which is equal to ‘npos’, hence the comparison.

It is also possible to use the index position to loop through a string, one character at a time, using a ‘for’ loop.

string example = "This is a string.";

for (size_t i = 0; i < example.length(); ++i)
    cout << example[i] << endl;

The above example uses the ‘length’ method of the string in the condition of the loop to limit the number of iterations through it. The resulting output displays each character of the string on a separate line.

This can be simplified by using a range based ‘for’ loop in a similar fashion to arrays.

string example = "This is a string.";

for (char letter: example)
    cout << letter << endl;


Using the ‘substr’ method, it is possible to return a portion of a string by specifying the index position to start from, together with the number of characters that are required.

string example = "This is a string.";

cout << example.substr(5, 4) << endl;

The above example starts at index position five and displays four characters.

is a


If it is necessary to add one or more characters into an existing string, the ‘insert’ method can be utilised to achieve this.

string example = "This is a string.";
example.insert(10, "short ");

cout << example << endl;

The ‘insert’ method requires two parameters, the index position where the characters need to be inserted, together with the characters that need to be incorporated.  In the case of the above example, the string ‘short ‘ is inserted into the example string, starting at index position ten.

This is a short string.


As the name suggests, the ‘replace’ method can be used to substitute one or more characters with other characters.

string example = "This is a short string.";
example.replace(10, 5, "small");

cout << example << endl;

This method takes three parameters, the index position to start from, the number of characters to replace and what characters are to be included instead. Unlike in this example, where the word ‘short’ is replaced with ‘small’, the number of characters substituted in do not need to be the same as the number that need to be replaced.

This is a small string.


The ‘erase’ method does the opposite of ‘insert’, it removes a specified number of characters.

string example = "This is a short string.";
example.erase(10, 6);

cout << example << endl;

In the above example, six characters are removed, starting at index position ten.

This is a string.