Converting std::string to Unicode String in c++
16,310
Solution 1
Form1->Memo1->Lines->Add("some text "+infstring.c_str()+" some text");
should be
Form1->Memo1->Lines->Add(("some text "+infstring+" some text").c_str());
i.e. you add the string literals to the std::string
then use c_str()
to get a const char*
from it.
That still won't work if the Add()
function takes a different type, but you haven't given enough information to know what you're asking about.
Solution 2
use a stringstream
#include <sstream>
std::stringstream ss;
ss << "some text" << mystring << "some text";
Form1->Memo1->Lines->Add(ss.str().c_str());
Related videos on Youtube
Author by
user2090826
Updated on October 06, 2022Comments
-
user2090826 over 1 year
I have a problem. I need to convert string type to unicode. I know metod like
string.c_str();
but it doesn't work in my code.
I have function
void modify(string infstring, string* poststring)
and in it i need to display infstring in memo. Like a
Form1->Memo1->Lines->Add("some text "+infstring.c_str()+" some text");
but compiler says me "E2085 Invalid Pointer addition"
How can i solve my problem?
-
Jonathan Wakely about 11 yearsWhat do you mean "Unicode String"? That's not a C++ type, what type do you mean?
-
-
Remy Lebeau about 11 yearsThe OP is most likely using C++Builder (the syntax matches its VCL UI framework), in which case an alternative would be this:
Form1->Memo1->Lines->Add("some text " + String(infstring.c_str(), infstring.length()) + " some text");
The VCL'sString
class (uppercaseS
), whichAdd()
takes as input, accepts achar*
and optional length in its constructor, and can be concatenated withchar*
values to create temporaryString
instances.