zoukankan      html  css  js  c++  java
  • Cocos2d-x中的字符串

    在Cocos2d-x中能够使用的字符串constchar*、std::string和cocos2d::__String等,其中const char*是C风格的字符串,std::string是C++风格的字符串,它封装了const char*。cocos2d::__String才是Cocos2d-x引擎提供的字符串类,这些字符串都可以互相转换,它们会在不同的场景下使用,具体使用那个可以看具体的API。

    使用const char*std::string

    我们在C++中两种类型都可以使用,但是std::string是一个类,具体面向对象的优点,而const char*没有。我们是下面代码初始化std::string对象。

            

    [html] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. std::string name = "tony";  
    2. td::string name = std::string("tony");  

     

    我们不需要使用指针,也不需要关心内存释放问题,在作用域超出之后std::string对象别释放。我们可以通过下面的语句把std::string转化为const char*类型。

     

    [html] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. const char* cstring = name.c_str();  

    我们可以使用std::string指针类型,但是要配合使用new关键字开辟内存空间,然后不再使用的时候要通过delete释放内存。

     

    [html] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. std::string* name =newstd::string("tony");  
    2. … …  
    3. delete name;  

     

    使用std::string指针对象时候,我们可以通过下面的代码转化为const char*类型。

       

    [html] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. const char* cstring = name->c_str();  

    const char* std::string的在Cocos2d-x中还有很多,我们会在后面的学习中给大家介绍。

    使用cocos2d::__String

    cocos2d::__StringCocos2d-x通过的一个字符串类,它的设计模拟了Objective-CNSString类,这由于Cocos2d-x源自于Cocos2d-iphone,cocos2d::__String也是基于Unicode双字节编码。

    cocos2d::__String的类图如下图所示,


    创建它的主要的静态create函数如下:

    [html] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. static__String * create (const std::string &str)  
    2. static__String * createWithFormat (const char *format,...)  

     

    使用create函数的实例代码如下:

     

    [html] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. __String* name=  __String::create("Hi,Tony");  
    2. int num=123;  
    3. __String* ns = __String::createWithFormat("%d",num);  
    4.    

    cocos2d::__String还提供了一些数据类型之间的转换函数。例如:cocos2d::__String转换为const char*类型,这种转换用的比较多的,示例代码如下:

     

    [html] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. __String* name=  __String::create("Hi,Tony");  
    2. const char *cstring=name->getCString();  

     

    const char*转换为cocos2d::__String类型,示例代码如下:

     

    [html] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. const char* cstring = "Hi,Tony";  
    2. __String*ns=__String::createWithFormat("%s",cstring);  

     

    std::string转换为cocos2d::__String类型,示例代码如下:

     

    [html] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. std::string string = "Hi,Tony";     
    2. __String*ns=__String::createWithFormat("%s",string.c_str());  

     

    cocos2d::__String转换为int类型,示例代码如下:

     

    [html] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. int num = 123;  
    2. __String* ns =__String::createWithFormat("%d",num);  
    3. int num2 = ns->intValue();  

     

    还有很多函数我们会在以后的学习再给大家介绍。

  • 相关阅读:
    数据可视化 —— 数据流图(Data Flow Diagram)
    TensorFlow 实战(四)—— tensor 的认识
    数据集(benchmark)、常用数据集的解析(cifar-10、)
    HDU--杭电--4504--威威猫系列故事——篮球梦--DP
    android打包apk时混淆遇到的问题
    C语言数据结构----递归的应用(斐波拉契数列、汉诺塔、strlen的递归算法)
    按 Eclipse 开发喜好重新布置 cocos2dx 目录层次
    HDU--杭电--4502--吉哥系列故事——临时工计划--背包--01背包
    (step6.3.2)hdu 1068(Girls and Boys——二分图的最大独立集)
    flashcache中应用device mapper机制
  • 原文地址:https://www.cnblogs.com/iOS-Blog/p/3718627.html
Copyright © 2011-2022 走看看