In this tutorial, we will talk about the C++ StringBuilder class with examples.
What is StringBuilder in C++?
The “<sstream>” header gives us a stream-based interface that can be used to perform input and output operations using the class “std::stringstream” or “std::ostringstream” with their objects, which are designed specifically for string concatenation. The stringstream class provides an easy way to manipulate strings using stream operations.
Syntax
The header which is used to access the StringBuiler class is as follows:
The objects of StringBrilder classes are mentioned below:
ostringstream str1;
The dot(.) operator is used for accessing the class objects.
Note: You can quickly write, read, and alter strings using the “stringstream” class. Like the “cout” and “cin” streams, it allows access and modifies formatted data. Developers can use the insertion “<<” operator to pull data from the stream and the “>>” operator to put data into the stream.
Now, we will move towards the simple implementation of a C++ example that demonstrates the “StringBuilder” class known as stringstream.
Example 1: StringBuilder With “stringstream”
The StringBuilder class can be used with the stringstream class. Here is an example:
#include <sstream>
#include <string>
using namespace std;
int main() {
ostringstream str1;
string name = "Hazal";
int age = 12;
str1 << "The name is: " << name << endl;
str1 << "The age is: " << age << endl;
cout << str1.str() << endl;
return 0;
}
In the above-stated example, we used the “ostringstream” class to generate an object with the name “str1” which is a StringBuilder. Then, initialized the “name” and “age” variables, and appended them to the “str1” string using the “<<” operator. Finally, we used the “str1.str()” method to push the combined string to the console to display the output as provided below:
Example 2: StringBuilder With “stringstream” to Manipulate String
Another example to use the “std::stringstream” for adding data in a string is described below:
#include <sstream>
#include <string>
int main() {
std::stringstream my_string;
std::string name = "Sana";
int age = 24;
// Insert data into the string stream
my_string << "My name is " << name << " and I am " << age << " years old.";
// Get the string from the string stream
std::string my_result = my_string.str();
// Print the result
std::cout << my_result << std::endl;
return 0;
}
In the above example, we created an object named “my_string” from a “std::stringstream”. Then, used the insertion operator “<<” to insert prepared data into the “stringstream”. After that, we used the “str()” method to extract the string from the “stringstream” and save it in the “my_result” variable. Lastly, used the “std::cout” to print the outcome, which is as follows:
Example 3: Create, Read, and Modify a String Using StringBuilder “stringstream” Class
To create, read, and modify a string using the StringBuilder stringstream class, we created an “str1” object from a “std::stringstream”. Then, used the insertion operator “<<” to add data that has been prepared into the stringstream. After that, added the “my_name” and “my_age” variable values to the stringstream. To extract the string through the stringstream, invoked the “str()” method. At last, print the result using the “std::cout” statement:
#include <sstream>
#include <string>
int main() {
std::stringstream str1;
std::string my_name = "Hamza";
int my_age = 22;
// Insert data into the string stream
str1 << "My name is " << my_name << " and I am " << my_age << " years old.";
// Get the string from the stringstream
std::string final_result = str1.str();
// Print the result
std::cout << "The inserted string is :" <<final_result << std::endl;
// Clear the stringstream
str1.str("");
// Modify the string
my_name = "Ali";
my_age = 25;
// Insert new data into the stringstream
str1 << "My name is " << my_name << " and I am " << my_age << " years old.";
// Get the modified string from the string stream
final_result = str1.str();
// Print the changed string
std::cout << "The modified string is :" <<final_result << std::endl;
return 0;
}
The output is:
We have briefly discussed the StringBuilder class in C++.
Conclusion
In C++, the StringBuilder class is a valuable tool for concatenating strings. It is known as “stringstream”. It is an effective way in many programming settings to work with strings utilizing stream operations. By using the stringstream class, developers can create, read, and alter strings with ease. This guide elaborated on the StringBuilder class in C++.