12 C++ String Methods You Should Master Today

12 C++ String Methods You Should Master Today

Originally posted on makeuseof.

Handle strings like a pro with these C++ methods.

C++ is one of the most powerful programming languages with the help of its built-in methods to perform operations like sorting, searching, and reversing. These methods cover the ease-of-use drawbacks C++ has when compared to other high-level programming languages like Java and Python.

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:

#include <string>

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

string str = “Welcome To MUO”;

RELATED:JavaScript String Methods You Should Master Today

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:

auto i = str.begin();
cout<<"The first character in the string str is: "<<*i<<endl;


The first character in the string str is: W 

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:

auto i = s.end()-1;
cout<<"The last character in the string s is: "<<*i<<endl;


The first character in the string s is: O

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

   for(auto i = str.begin(); i!= str.end(); i++){

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:

Welcome To MUO!

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:

string str2 = " Hi there";
for(auto i = str2.begin(); i!=str2.end() ;i++){


Welcome To MUO! Hi there

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:



Welcome To MU

5. size()

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

cout<<"The size of the string str is "<<str.size()<<endl;

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.

char str2[50];
str.copy(str2, 6,2);
cout<<"The value in str2: "<<str2<<endl;


The value in str2: lcome

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.

string str = “Welcome To MUO”;
string str2 = "Hi There";   
cout<<"String 1 str: "<<str<<endl;
cout<<"String 2 str2: "<<str2<<endl;


String 1 str: Hi There
String 2 str2: Welcome To MUO

RELATED:How To Swap Two Variables In C++, Python, And JavaScript

8. getline()

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

string s;
cout<<"Enter a string"<<endl;


Enter a string
Welcome to MUO

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.

cout<<"The value of str after resizing it: "<<str<<endl;


The value of str after resizing it: Welcome To

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.

cout<<"The capacity of the string is "<<str.capacity()<<endl;

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:

string s1 = "123";
int v1 = stoi(s1);
cout<<"Stoi() for s1: "<<v1<<endl;
string s2 = "123 pass";
int v2 = stoi(s2);
cout<<"Stoi() for s2: "<<v2;


Stoi() for s1: 123 
Stoi() for s2: 123

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.

auto beg = str.rbegin();
auto end = str.rend()-1;
cout<<"The last character is: "<<*beg<<endl;
cout<<"The first character is: "<<*end<<endl;

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.

for(auto i=str.rbegin(); i!=str.rend(); i++){


The last character is: O
The first character is: W
OUM oT emocleW

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.


Source: makeuseof