12 C++ String Methods You Should Master Today
12 C String Methods You Should Master Today
Consider an example string variable str with the value of "Welcome To MUO" to implement these methods. string str = Welcome To MUO;
coutThe first character in the string str is: *iendl;
Output: The first character the string str : W
coutThe last character in the string s is: *iendl;
Output: The first character the string s : 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++){
cout*i;
}
coutstr;
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++){
(*);
}
coutstrendl;
Output: Welcome To MUO! Hi there
coutstrendl;
Output: Welcome To MU
(, 6,2);
coutThe value in str2: str2endl;
Output: The value in str2: lcome
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;
();
coutString 1 str: strendl;
coutString 2 str2: str2endl;
Output: str: Hi There
str2: Welcome To MUO
coutEnter a stringendl;
getline(cin,s);
coutsendl;
Output: Enter a string
Welcome to MUO
coutThe value of str after resizing it: strendl;
Output: The value of str after resizing it: Welcome To
v1 = stoi(s1);
coutStoi() for s1: v1endl;
string s2 = 123 pass;
v2 = stoi(s2);
coutStoi() for s2: v2;
Output: Stoi() for s1: 123
Stoi() for s2: 123
auto = str.rend();
coutThe last character is: *begendl;
coutThe first character is: *endendl;
You can also print the 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++){
cout*i;
}
Output: The last character : O
The first character : W
OUM oT emocleW
MUO
12 C String Methods You Should Master Today
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 stringConsider an example string variable str with the value of "Welcome To MUO" to implement these methods. string str = Welcome To MUO;
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();coutThe first character in the string str is: *iendl;
Output: The first character the string str : 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();coutThe last character in the string s is: *iendl;
Output: The first character the string s : 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++){
cout*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. str.push_back(!);coutstr;
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++){
(*);
}
coutstrendl;
Output: 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: ();coutstrendl;
Output: Welcome To MU
5 size
The size() method helps you calculate the length of the string. coutThe 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. str2[];(, 6,2);
coutThe value in str2: str2endl;
Output: 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;
();
coutString 1 str: strendl;
coutString 2 str2: str2endl;
Output: str: Hi There
str2: Welcome To MUO
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;coutEnter a stringendl;
getline(cin,s);
coutsendl;
Output: 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. (10);coutThe value of str after resizing it: strendl;
Output: 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. coutThe 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;v1 = stoi(s1);
coutStoi() for s1: v1endl;
string s2 = 123 pass;
v2 = stoi(s2);
coutStoi() for s2: v2;
Output: 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 = str.rend();
coutThe last character is: *begendl;
coutThe first character is: *endendl;
You can also print the 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++){
cout*i;
}
Output: The last character : O
The first character : W
OUM oT emocleW