Windows 8 C++/CX字符串

在C++/CX里面是使用Platform::String类来表示字符串的类型,在windows运行时的接口和方法中,需要使用Platform::String来作为字符串参数的传递。如果需要使用标准C++的字符串类型如wstring或者string的时候,可以将Platform::String与标准的C++的字符串进行互相的转换。

String类型的构造

String类型表示的是char16的字符串,可以直接通过字符串的赋值来进行构造也可以使用标准C++的wchar_t*指针进行构造。

  1. // Initializing a String^ by using string literals
  2. String^ str1 = "Test"; // ok for ANSI text. uses current code page
  3. String^ str2("Test");
  4. String^ str3 = L"Test";
  5. String^ str4(L"Test");
  6. //Initialize a String^ by using another String^
  7. String^ str6(str1);
  8. auto str7 = str2;
  9. // Initialize a String from wchar_t* and wstring
  10. wchar_t msg[] = L"Test";
  11. String^ str8 = ref new String(msg);
  12. std::wstring wstr1(L"Test");
  13. String^ str9 = ref new String(wstr1.c_str());
  14. String^ str10 = ref new String(wstr1.c_str(), wstr1.length());

字符的操作

String提供了相关的方法来操作字符串,其中可以使用String::Data()方法来返回一个String^ 对象的wchar_t*指针。

  1. // Concatenation
  2. auto str1 = "Hello" + " World";
  3. auto str2 = str1 + " from C++/CX!";
  4. auto str3 = String::Concat(str2, " and the String class");
  5. // Comparison
  6. if (str1 == str2) { /* ... */ }
  7. if (str1->Equals(str2)) { /* ... */ }
  8. if (str1 != str2) { /* ... */ }
  9. if (str1 < str2 || str1 > str2) { /* ... */};
  10. int result = String::CompareOrdinal(str1, str2);
  11. if(str1 == nullptr) { /* ...*/};
  12. if(str1->IsEmpty()) { /* ...*/};
  13. // Accessing individual characters in a String^
  14. auto it = str1->Begin();
  15. char16 ch = it[0];

String类型的转换

String类型可以和标准C++的wstring进行互相的转换

  1. // compile with: /ZW
  2. #include <string>
  3. using namespace std;
  4. using namespace Platform;
  5. int main( array<String^>^ args )
  6. {
  7. // Create a String^ variable statically or dynamically from a
  8. literal string.
  9. String^ str1 = "AAAAAAAA";
  10. // Use the value of str1 to create the ws1 wstring variable.
  11. wstring ws1( str1->Data() );
  12. // The value of ws1 is L"AAAAAAAA".
  13. // Manipulate the wstring value.
  14. wstring replacement( L"BBB" );
  15. ws1ws1 = ws1.replace ( 1, 3, replacement );
  16. // The value of ws1 is L"ABBBAAAA".
  17. // Assign the modified wstring back to str1.
  18. str1 = ref new String( ws1.c_str() );
  19. return 0;
  20. }

Database error: [Table 'ac_search_cache' is marked as crashed and should be repaired]

SELECT * FROM ac_search_cache WHERE hash = 'de395958ffcecac4bb556dd43f9659483196a463' LIMIT 1;