. Advertisement .
..3..
. Advertisement .
..4..
If you want to convert a char to a string in C++, there are plenty of options to pick from.
Convert A Char To A String In C++
With std::string Constructor
The string class in C++ has its own constructor that can be used to create a string object. By default, it constructs an empty string, meaning the length of the string is zero. You can set this length to one and assign a character to this string at the same time with this syntax:
string s(size_t n, char x);
Parameters:
- n: the number of characters
- x: the substring you want to copy into your new string object
Example:
#include <iostream>
#include <string>
int main()
{
char c = 'I';
std::string s(1, c);
std::cout << s << "\n";
return 0;
}
Output:
I
This program uses the std::string
constructor to construct a string object called s. The argument 1 means this string has a length of one character, while the argument c tells the constructor to copy the value of c.
With std::string::operator+=
The +=
operator can extend a current string object by inserting extra characters at the end of the string. Its syntax to append a character:
string& operator+= (char c);
In which c is a character that will be appended at the end of the current value.
Example:
#include <iostream>
#include <string>
int main ()
{
char c = 'I';
std::string name;
std::cout << "Before: " << name <<"\n";
name += c;
std::cout << "After: " << name <<"\n";
return 0;
}
Output:
Before:
After: I
The first std::cout
statement shows that the string name is empty at first. But after we apply the +=
operator with the character variable c, its value has been appended with the character we want.
With std::string::operator=
Likewise, you can use the =
operator to replace the current contents of a string and assign a new value, such as a character. Syntax:
string& operator= (char c);
In which c is the character you want to copy to the current string. After this operation, the length of the string will become one.
Example:
#include <iostream>
#include <string>
int main ()
{
std::string name ("ITTutoria");
char c = 'I';
std::cout << "Before: " << name <<"\n";
name = c;
std::cout << "After: " << name <<"\n";
return 0;
}
Output:
Before: ITTutoria
After: I
As you can see, the assignment operator doesn’t care about the current content of the string. It just replaces it completely with your character.
With std::stringstream
The std namespace has a stream class called stringstream that you can use to operate on strings. You can create objects of this class, which uses a string buff to contain a sequence of characters. You can directly access this sequence as a string object as well as insert characters into this stream with regular operations that are allowed on input and output streams.
Example:
#include <iostream>
#include <string>
#include <sstream>
int main ()
{
std::string name;
char c = 'I';
std::stringstream ss;
std::cout << "Before: " << name <<"\n";
ss << c;
ss >> name;
std::cout << "After: " << name <<"\n";
return 0;
}
Output:
Before:
After: I
Remember to include the sstream header file in order to use the stringstream class. The program above inserts the character “I” into the stream first and then writes its buffer into the string object, all using standard read/write operations.
Conclusion
There is no shortage of ways to convert a char to a string in C++. Most of them come from the string class and allow you to modify an existing string object. If you want to go further and convert strings to hex, check out this guide.
Leave a comment