如何在 c++/cx 中 trim Platform::String myString = " answer "

How to trim Platform::String myString = " answer " in c++/cx

我想在 c++/cx 中 trim 字符串,我正在使用 Platform::String

Platform::String myString = "  answer "

Platform::String 是 Windows 运行时 HSTRING 类型的包装器,它是不可变的。理想情况下,您应该只在需要它的 Windows 运行时组件的 public 接口中使用 Platform::String,否则请考虑 std::wstring 修改字符串的操作,例如 [=33] =]明.

documentation所述:

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. It's easy and efficient to convert between wstring or wchar_t* and Platform::String.

您可以使用以下代码从 Platform::String 转换为 wstring,然后是 trim,然后从结果转换创建新的 Platform::String

Platform::String^ myString = ref new Platform::String(L"  answer ");
//convert to wstring
std::wstring classicString(myString->Data());

//trimming
classicString.erase(0, classicString.find_first_not_of(' '));
classicString.erase(classicString.find_last_not_of(' ') + 1);

//convert to Platform::String
Platform::String^ modifiedString = ref new Platform::String(classicString.c_str());

有关trimming a wstring的更多相关解决方案,请参阅this SO post