In this article, you’ll learn 12 C++ string methods that help you perform operations on strings in a fraction of the code you’ve been using.

What Are String Methods in C++?

String methods are the pre-built functions stored in the string header file. You can use them by importing the string header file:

Consider an example string variable str with the value of “Welcome To MUO” to implement these methods.

1. begin()

The begin() method in C++ returns an iterator to the beginning of the string. Create an iterator using the auto keyword and store the initial reference of the string variable using str.begin(). The code below shows the implementation:

Output:

2. end()

The end() string method returns the iterator to the end of the string. This code prints the last character of the string variable:

Output:

You can also loop through the string and print individual characters using the begin() and end() methods. Here’s how :

3. push_back()

The push_back() method inserts a character to the end of the string. By performing this operation, the string’s size increases by 1.

The output of the code above will have an exclamation (!) mark along with the original string:

You can also append a set of characters or another string by looping through and appending it character by character. Consider a string variable str2, with the value of “ Hi there”. To append this variable to the original string using the push_back() method:

Output:

4. pop_back()

The pop_back() method removes the last character of a string. Here’s how you can try this method on the string str:

Output:

5. size()

The size() method helps you calculate the length of the string.

6. copy()

The copy() method copies a complete string or sub-string. It accepts three arguments: character array, length of substring, and the position where the string should start copying from.

Output:

7. swap()

The swap() method helps you swap two strings with each other. The syntax for this method is:

This method accepts a string variable as an argument. You can run this method on the string you want to swap and print to check the results.

Output:

8. getline()

The getline() method stores a stream of characters accepted during input. This method accepts two arguments: cin and the string variable.

Output:

9. resize()

The resize() method changes the length of the string by dynamically increasing or decreasing it. It accepts one argument: the length to which you want to resize your string.

Output:

10. capacity()

The capacity() method in C++ returns the capacity allocated to the string. It can be equal to the length of the string or greater than it.

11. stoi()

The stoi() method helps convert a number in the form of a string to its numeric value. It accepts one parameter: the string variable. If your string variable has other characters apart from numbers, it will filter them out. But for this method to work, the non-numerical string characters must follow the numbers. If the characters appear before the numbers, it’ll return an error.

Before going through with the above operation, make sure that you store it in an integer variable before printing it. Here’s an example:

Output:

12. rbegin() and rend()

The rbegin() method returns the reference of the reverse iterator to the string at the end. Similarly, the rend() method returns the reference of the start iterator to the string at the beginning.

You can also print the reverse of the string using rbegin() and rend() methods. To do so, you can loop through the string and print it character by character.

Output:

Take a Step Ahead in Learning C++

Now that you’ve learned to implement various string methods of the string header, you can feel confident exploring more pre-built methods in the C++ header files. From sort() and reverse() to binary_search(), there’s little C++ STL can’t accomplish in the world of coding.

Taking the time to learn about the Standard Template Library is an excellent investment for C++ programmers of all levels. The Standard Template Library provides built-in functions, common data structures, and handy algorithms to make programming easy and efficient.