std::string class in C++

06 Jul, 2022
C++ has in its definition a way to represent a sequence of characters as an object of the class. This class is called std:: string. String class stores the characters as a sequence of bytes with the functionality of allowing access to the single-byte character.
String
Char Array
Example:
CPP
// C++ Program to demonstrate the working of// getline(), push_back() and pop_back()#include <iostream>#include <string> // for string classusing namespace std;  // Driver Codeint main(){    // Declaring string    string str;      // Taking string input using getline()    getline(cin, str);      // Displaying string    cout << "The initial string is : ";    cout << str << endl;      // Inserting a character    str.push_back('s');      // Displaying string    cout << "The string after push_back operation is : ";    cout << str << endl;      // Deleting a character    str.pop_back();      // Displaying string    cout << "The string after pop_back operation is : ";    cout << str << endl;      return 0;}
Output: 
The initial string is : geeksforgeek
The string after push_back operation is : geeksforgeeks
The string after pop_back operation is : geeksforgeek
Example:
CPP
// C++ Program to demonstrate the working of// capacity(), resize() and shrink_to_fit()#include <iostream>#include <string> // for string classusing namespace std;  // Driver Codeint main(){    // Initializing string    string str = "geeksforgeeks is for geeks";      // Displaying string    cout << "The initial string is : ";    cout << str << endl;      // Resizing string using resize()    str.resize(13);      // Displaying string    cout << "The string after resize operation is : ";    cout << str << endl;      // Displaying capacity of string    cout << "The capacity of string is : ";    cout << str.capacity() << endl;      // Displaying length of the string    cout << "The length of the string is :" << str.length()         << endl;      // Decreasing the capacity of string    // using shrink_to_fit()    str.shrink_to_fit();      // Displaying string    cout << "The new capacity after shrinking is : ";    cout << str.capacity() << endl;      return 0;}
The initial string is : geeksforgeeks is for geeks
The string after resize operation is : geeksforgeeks
The capacity of string is : 26
The length of the string is :13
The new capacity after shrinking is : 15
Example:
CPP
// C++ Program to demonstrate the working of// begin(), end(), rbegin(), rend()#include <iostream>#include <string> // for string classusing namespace std;  // Driver Codeint main(){    // Initializing string`    string str = "geeksforgeeks";      // Declaring iterator    std::string::iterator it;      // Declaring reverse iterator    std::string::reverse_iterator it1;      // Displaying string    cout << "The string using forward iterators is : ";    for (it = str.begin(); it != str.end(); it++)        cout << *it;    cout << endl;      // Displaying reverse string    cout << "The reverse string using reverse iterators is "            ": ";    for (it1 = str.rbegin(); it1 != str.rend(); it1++)        cout << *it1;    cout << endl;      return 0;}
The string using forward iterators is : geeksforgeeks
The reverse string using reverse iterators is : skeegrofskeeg
4) Manipulating Functions:
Example:
CPP
// C++ Program to demonstrate the working of// copy() and swap()#include <iostream>#include <string> // for string classusing namespace std;  // Driver Codeint main(){    // Initializing 1st string    string str1 = "geeksforgeeks is for geeks";      // Declaring 2nd string    string str2 = "geeksforgeeks rocks";      // Declaring character array    char ch[80];      // using copy() to copy elements into char array    // copies "geeksforgeeks"    str1.copy(ch, 13, 0);      // Displaying char array    cout << "The new copied character array is : ";    cout << ch << endl;      // Displaying strings before swapping    cout << "The 1st string before swapping is : ";    cout << str1 << endl;    cout << "The 2nd string before swapping is : ";    cout << str2 << endl;      // using swap() to swap string content    str1.swap(str2);      // Displaying strings after swapping    cout << "The 1st string after swapping is : ";    cout << str1 << endl;    cout << "The 2nd string after swapping is : ";    cout << str2 << endl;      return 0;}
The new copied character array is : geeksforgeeks
The 1st string before swapping is : geeksforgeeks is for geeks
The 2nd string before swapping is : geeksforgeeks rocks
The 1st string after swapping is : geeksforgeeks rocks
The 2nd string after swapping is : geeksforgeeks is for geeks
Must Read: C++ String Class and its Applications
YouTube<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=3rDp0yOACZQ" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above. 
Fenrear
keshariashwani678
shettykaran21
thepranaygupta
as5853535
johnakup02
anshikajain26
surbhikumaridav
harsh_shokeen
cpp-string
cpp-strings-library
STL
C Language
C++
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.