60

Simple question (in C++):

How do I convert a single character into a string. So for example, I have string str = "abc";

And I want to extract the first letter, but I want it to be a string as opposed to a character.

I tried

string firstLetter = str[0] + ""; 

and

string firstLetter = & str[0]; 

Neither works. Ideas?

2

10 Answers 10

72

Off the top of my head, if you're using STL then do this:

string firstLetter(1,str[0]); 
Sign up to request clarification or add additional context in comments.

3 Comments

Note that std::string is not from that part of the standard library that was derived from the STL.
Adding this comment to clear up what the syntax indicates. This basically says, "I want a string named firstLetter that grabs 1 character starting from str's first ([0]) element." en.cppreference.com/w/cpp/string/basic_string/basic_string
@kayleeFrye_onDeck - actually it doesn't. It says "give me 1 copy of the given character". It can't "start" from the first element of str, because str isn't even passed, rather the character at str[0] is passed by value. If you use 2 instead of 1 you'll get two copies of the first character, not the first two characters. That's directly from your link where you can see it's form (2) of the constructor, documented as: 2) Constructs the string with count copies of character ch. The behavior is undefined if count >= npos.
25

You can use the std::string(size_t , char ) constructor:

string firstletter( 1, str[0]); 

or you could use string::substr():

string firstletter2( str.substr(0, 1)); 

Comments

6

1) Using std::stringstream

 std::string str="abc",r; std::stringstream s; s<<str[0]; s>>r; std::cout<<r; 

2) Using string ( size_t n, char c ); constructor

 std::string str="abc"; string r(1, str[0]); 

3) Using substr()

 string r(str.substr(0, 1)); 

1 Comment

Also, this will work, too: string r(&str[0], 1); or string r(str.c_str(), 1);
4
string s; char a='c'; s+=a; //now s is "c" 

or

char a='c'; string s(1, a); //now s is "c" 

2 Comments

I think my solution is correct, don't know why I got a -1.
std::string does not have a constructor that accepts a single char as input by itself, so string s(a); will not work. However, string s(1, a); and string s(&a, 1); will work instead.
3

Use string::substr.

In the example below, f will be the string containing 1 characters after offset 0 in foo (in other words, the first character).

string foo = "foo"; string f = foo.substr(0, 1); cout << foo << endl; // "foo" cout << f << endl; // "f" 

Comments

3
char characterVariable = 'z'; string cToS(1, characterVariable); //cToS is now a string with the value of "z" 

Comments

2

string firstletter(str.begin(), str.begin() + 1);

Comments

0

you can try this it works

string s="hello"; string s1=""; s1=s1+s[0]; 

Comments

0

A lot of the other answers are correct, but with syntax that leaves a lot to be desired. As of C++11, you can use the initializer list constructor of std::string (overload (15) on cppreference).

With this constructor, turning the first char into a std::string named first_letter is as trivial as:

std::string first_letter{ str.front() }; 

Comments

-1

If you want to return a string, there 2 ways to do this is given below:

1.`return (string) "" + S[i];

2.string s.push_back(originals[k];return s;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.