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());
Share:
16,310

Related videos on Youtube

user2090826
Author by

user2090826

Updated on October 06, 2022

Comments

  • user2090826
    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
      Jonathan Wakely about 11 years
      What do you mean "Unicode String"? That's not a C++ type, what type do you mean?
  • Remy Lebeau
    Remy Lebeau about 11 years
    The 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's String class (uppercase S), which Add() takes as input, accepts a char* and optional length in its constructor, and can be concatenated with char* values to create temporary String instances.