![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Expert Programmer
|
char to string in C++
How do you initialize a string as a character in C++?
In Java you would use: char c = 'c'; String str = Character.toString(c); Also, how do you convert a character to a string in general? |
|
|
|
|
|
#2 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 754
Rep Power: 3
![]() |
There is a string constructor that takes a C-string and one that takes a char array and a length (c-string being a null-terminated char array or string literal). If you know what the letter you want is, you can just use:
string str("c");char c = /* whatever */; string str(&c, 1); |
|
|
|
|
|
#3 |
|
Expert Programmer
|
Thanks for your help.
Also, how do you append characters (chars) to a string? |
|
|
|
|
|
#4 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 754
Rep Power: 3
![]() |
string has an operator+= defined, which as I remember takes another string or the same char* arguments listed above
|
|
|
|
|
|
#5 |
|
Expert Programmer
|
Why doesn't this code give me the first 2 characters of str1 as a string?
string str1("27582347502458270");
string s(str1[0], 1);
s.append(&str1[1]);
cout << s << endl; |
|
|
|
|
|
#6 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 754
Rep Power: 3
![]() |
first off, remember that the constructor takes a char* not a char (as operator[] returns), so the 2nd line should probably be
string s(str1.c_str(),1); // get the char* // or an alternative string s(&str[0], 1); // get a pointer to the first char for the append, unless you specifiy a length, it will append data until it reaches a null character (with integer value 0). Since the string is (I believe) stored as a char* in memory, it keeps reading until it reaches the end, hence you have more than 2 characters. That's easily solved with s.append(&str[1], 1); |
|
|
|
|
|
#7 |
|
Expert Programmer
|
Thanks again, Jimbo.
Is there a website that has the official documentation for C++? For Java there is http://java.sun.com/j2se/1.3/docs/api/java/lang/. Thanks. |
|
|
|
|
|
#8 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 754
Rep Power: 3
![]() |
I usually just use MSDN, especially since I use Visual Studio. I believe Microsoft's implementation is starting to follow the standard well enough to use it as a general reference anyways. Otherwise, just google around. I think there's another website like cplusplus.com or something like that, which has some decent documentation on a few standard header functions. I don't think there's a standard site, however.
By the way, your link for Java is a bit old... http://java.sun.com/j2se/1.5.0/docs/api/ is a little more like it... |
|
|
|
|
|
#9 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
IIRC, you can just do this, since the stl string overloads the = operator:
char *str = "Some C-style string"; string cppStr = str; |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|