zoukankan      html  css  js  c++  java
  • Windows 8 C++/CX字符串

         在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;
    }
  • 相关阅读:
    asp.net中virtual和abstract的区别分析
    .NET中的Timer类型用法详解
    类型参数的约束(C# 编程指南)T
    SELECT INTO 和 INSERT INTO SELECT 两种表复制语句
    jquery的$.extend和$.fn.extend作用及区别
    类型参数约束 : Controller where T : class,new()
    asp.net获取当前网址url的各种属性(文件名、参数、域名 等)的代码
    字符串一级指针的内存模型图(盲点,以前自己懵懂)
    字符串的基本操作,初始化和赋值之类的区别,和数据名是一个常量指针不可以改变和赋值(盲点众多)
    关于内存四区和指针的修改问题
  • 原文地址:https://www.cnblogs.com/linzheng/p/2614014.html
Copyright © 2011-2022 走看看