Windows 8 C++/CX字串

來源:互聯網
上載者:User

     在C++/CX裡面是使用Platform::String類來表示字串的類型,在windows運行時的介面和方法中,需要使用Platform::String來作為字串參數的傳遞。如果需要使用標準C++的字串類型如wstring或者string的時候,可以將Platform::String與標準的C++的字串進行互相的轉換。

 

String類型的構造

String類型表示的是char16的字串,可以直接通過字串的賦值來進行構造也可以使用標準C++的wchar_t*指標進行構造。

// Initializing a String^ by using string literals    String^ str1 = "Test"; // ok for ANSI text. uses current code page    String^ str2("Test");    String^ str3 = L"Test";    String^ str4(L"Test");    //Initialize a String^ by using another String^    String^ str6(str1);    auto str7 = str2;    // Initialize a String from wchar_t* and wstring    wchar_t msg[] = L"Test";    String^ str8 = ref new String(msg);    std::wstring wstr1(L"Test");    String^ str9 = ref new String(wstr1.c_str());    String^ str10 = ref new String(wstr1.c_str(), wstr1.length());

 

字元的操作

 String提供了相關的方法來操作字串,其中可以使用String::Data()方法來返回一個String^ 對象的wchar_t*指標。

    // Concatenation     auto str1 = "Hello" + " World";    auto str2 = str1 + " from C++/CX!";        auto str3 = String::Concat(str2, " and the String class");        // Comparison    if (str1 == str2) { /* ... */ }    if (str1->Equals(str2)) { /* ... */ }    if (str1 != str2) { /* ... */ }    if (str1 < str2 || str1 > str2) { /* ... */};    int result = String::CompareOrdinal(str1, str2);        if(str1 == nullptr) { /* ...*/};    if(str1->IsEmpty()) { /* ...*/};   // Accessing individual characters in a String^    auto it = str1->Begin();    char16 ch = it[0];

 

String類型的轉換

String類型可以和標準C++的wstring進行互相的轉換

// compile with: /ZW#include <string>using namespace std;using namespace Platform;int main( array<String^>^ args ) {    // Create a String^ variable statically or dynamically from a literal string.     String^ str1 = "AAAAAAAA";        // Use the value of str1 to create the ws1 wstring variable.    wstring ws1( str1->Data() );     // The value of ws1 is L"AAAAAAAA".    // Manipulate the wstring value.    wstring replacement( L"BBB" );    ws1 = ws1.replace ( 1, 3, replacement );    // The value of ws1 is L"ABBBAAAA".    // Assign the modified wstring back to str1.     str1 = ref new String( ws1.c_str() );     return 0;}

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.