在 C++ 中将 std::string 转换为 Unicode 字符串

use*_*826 3 c++ string unicode c++builder

我有个问题。我需要将字符串类型转换为 unicode。我知道方法像

string.c_str();
Run Code Online (Sandbox Code Playgroud)

但它在我的代码中不起作用。

我有功能

void modify(string infstring, string* poststring)
Run Code Online (Sandbox Code Playgroud)

在其中我需要在备忘录中显示 infstring。像一个

Form1->Memo1->Lines->Add("some text "+infstring.c_str()+" some text");
Run Code Online (Sandbox Code Playgroud)

但编译器说我“E2085 无效指针添加”

我该如何解决我的问题?

Jon*_*ely 5

Form1->Memo1->Lines->Add("some text "+infstring.c_str()+" some text");
Run Code Online (Sandbox Code Playgroud)

应该

Form1->Memo1->Lines->Add(("some text "+infstring+" some text").c_str());
Run Code Online (Sandbox Code Playgroud)

即您将字符串文字添加到std::string 然后用于c_str()从中获取 a const char*

如果Add()函数采用不同的类型,这仍然不起作用,但是您没有提供足够的信息来了解您要询问的内容。

  • OP 最有可能使用 C++Builder(语法与其 VCL UI 框架匹配),在这种情况下,另一种选择是:`Form1->Memo1->Lines->Add("some text " + String(infstring. c_str(), infstring.length()) + " some text");` VCL 的`String` 类(大写`S`),`Add()` 作为输入,接受`char*` 和可选长度在其构造函数中,并且可以与 `char*` 值连接以创建临时的 `String` 实例。 (4认同)