如何访问 Platform::String 中的单个字符?
How to access individual character in Platform::String?
如何访问 Platform::String^
中的单个字符?
我正在使用这种变量类型,因为它似乎是在通用 Windows 应用程序上将字符串写入 TextBlock
的唯一方法。
我试过以下方法获取个别字符无效:
String ^ str = "string";
std::string::iterator it = str->begin(); //Error: platform string has no member "begin"
std::string::iterator it = str.begin(); //Error: expression must have a class type
str[0] = 't' /*Error: expression must have a pointer-to-object or handle-to-C++/CX
mapping-array type*/
我将 String^ 放入名为 "textBlock" 的文本块中,如下所示:textBlock->Text = str;
我对修改 Platform::String
以外的方法持开放态度。我唯一的要求是字符串以可以放入 TextBox
的形式结尾
Platform::String 表示用于表示文本的 Unicode 字符的顺序集合。受控序列是不可变的:一旦构建,Platform::String
的内容将无法再修改。
如果您需要可修改的字符串,规范的解决方案是使用另一个字符串 class,并在调用 Windows 运行时或接收时转换 to/from Platform::String
字符串数据。这在 Strings (C++/CX):
下解释
The Platform::String Class provides methods for several common string operations, but it's not designed to be a full-featured string class. In your C++ module, use standard C++ string types such as wstring for any significant text processing, and then convert the final result to Platform::String^ before you pass it to or from a public interface.
您可以按如下方式重写您的代码示例:
// Use a standard C++ string as long as you need to modify it
std::wstring s = L"string";
s[0] = L't'; // Replace first character
// Convert to Platform::String^ when required
Platform::String^ ps = ref new Platform::String(s.c_str(), s.length());
对已提供的答案的补充:您还可以将 Platform::String^ 更改为 std::wstring,以便您可以对其进行修改,然后您可以随时将其更改回 Platform::String^
Platform::String^ str = "string";
wstring orig (str->Data());
orig[0] = L't';
str = ref new Platform::String(orig.c_str());
如何访问 Platform::String^
中的单个字符?
我正在使用这种变量类型,因为它似乎是在通用 Windows 应用程序上将字符串写入 TextBlock
的唯一方法。
我试过以下方法获取个别字符无效:
String ^ str = "string";
std::string::iterator it = str->begin(); //Error: platform string has no member "begin"
std::string::iterator it = str.begin(); //Error: expression must have a class type
str[0] = 't' /*Error: expression must have a pointer-to-object or handle-to-C++/CX
mapping-array type*/
我将 String^ 放入名为 "textBlock" 的文本块中,如下所示:textBlock->Text = str;
我对修改 Platform::String
以外的方法持开放态度。我唯一的要求是字符串以可以放入 TextBox
Platform::String 表示用于表示文本的 Unicode 字符的顺序集合。受控序列是不可变的:一旦构建,Platform::String
的内容将无法再修改。
如果您需要可修改的字符串,规范的解决方案是使用另一个字符串 class,并在调用 Windows 运行时或接收时转换 to/from Platform::String
字符串数据。这在 Strings (C++/CX):
The Platform::String Class provides methods for several common string operations, but it's not designed to be a full-featured string class. In your C++ module, use standard C++ string types such as wstring for any significant text processing, and then convert the final result to Platform::String^ before you pass it to or from a public interface.
您可以按如下方式重写您的代码示例:
// Use a standard C++ string as long as you need to modify it
std::wstring s = L"string";
s[0] = L't'; // Replace first character
// Convert to Platform::String^ when required
Platform::String^ ps = ref new Platform::String(s.c_str(), s.length());
对已提供的答案的补充:您还可以将 Platform::String^ 更改为 std::wstring,以便您可以对其进行修改,然后您可以随时将其更改回 Platform::String^
Platform::String^ str = "string";
wstring orig (str->Data());
orig[0] = L't';
str = ref new Platform::String(orig.c_str());