Cocos2d-x中的字符串
2014-05-30 16:01
267 查看
在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<span style="font-weight: normal;"><span style="font-size:14px;"> std::string name = "tony";
std::string name = std::string("tony");</span></span>
我们不需要使用指针,也不需要关心内存释放问题,在作用域超出之后std::string对象别释放。我们可以通过下面的语句把std::string转化为const char*类型。[html] view plaincopy
<span style="font-weight: normal;"><span style="font-size:14px;">const char* cstring = name.c_str();</span></span>
我们可以使用std::string指针类型,但是要配合使用new关键字开辟内存空间,然后不再使用的时候要通过delete释放内存。[html] view plaincopy
<span style="font-weight: normal;"><span style="font-size:14px;"> std::string* name =newstd::string("tony");
… …
delete name;</span></span>
使用std::string指针对象时候,我们可以通过下面的代码转化为const char*类型。 [html] view plaincopy
<span style="font-weight: normal;"><span style="font-size:14px;"> const char* cstring = name->c_str();</span></span>
const char* 和std::string的在Cocos2d-x中还有很多,我们会在后面的学习中给大家介绍。
使用cocos2d::__String
cocos2d::__String是Cocos2d-x通过的一个字符串类,它的设计模拟了Objective-C的NSString类,这由于Cocos2d-x源自于Cocos2d-iphone,cocos2d::__String也是基于Unicode双字节编码。cocos2d::__String的类图如下图所示,创建它的主要的静态create函数如下:
<span style="font-weight: normal;"><span style="font-size:14px;">static__String * create (const std::string &str) static__String * createWithFormat (const char *format,...)</span></span>
使用create函数的实例代码如下:
<span style="font-weight: normal;"><span style="font-size:14px;">__String* name= __String::create("Hi,Tony"); int num=123; __String* ns = __String::createWithFormat("%d",num); </span></span>
cocos2d::__String还提供了一些数据类型之间的转换函数。例如:cocos2d::__String转换为const char*类型,这种转换用的比较多的,示例代码如下:
<span style="font-weight: normal;"><span style="font-size:14px;">__String* name= __String::create("Hi,Tony"); const char *cstring=name->getCString();</span></span>
const char*转换为cocos2d::__String类型,示例代码如下:
<span style="font-weight: normal;"><span style="font-size:14px;">const char* cstring = "Hi,Tony"; __String*ns=__String::createWithFormat("%s",cstring);</span></span>
std::string转换为cocos2d::__String类型,示例代码如下:
<span style="font-weight: normal;"><span style="font-size:14px;">std::string string = "Hi,Tony"; __String*ns=__String::createWithFormat("%s",string.c_str());</span></span>
cocos2d::__String转换为int类型,示例代码如下:
<span style="font-weight: normal;"><span style="font-size:14px;">int num = 123; __String* ns =__String::createWithFormat("%d",num); int num2 = ns->intValue();</span></span>
还有很多函数我们会在以后的学习再给大家介绍。更多内容请关注Cocos2d-x系列图书《Cocos2d-x实战(卷Ⅰ):C++开发》
本书交流讨论网站:http://www.cocoagame.net
欢迎加入cocos2d-x技术讨论群:257760386、327403678
更多精彩内容请关注史上最牛的cocos2d-x课程《cocos2d-x手机游戏开发实战》直播课程第一期报名Cocos2d-x直播课程还送关东升老师iOS终身会员。
课程链接:http://edu.51cto.com/pack/view/id-13.html会员链接:http://edu.51cto.com/member/id-3.html
相关文章推荐
- cocos2d-x 中英文字符串截取
- 【Cocos2d-x】开发实战-Cocos中的字符串、标签和中文乱码
- Visual Studio里开发cocos2d-x时,代码含有中文字符串无法通过编译的原因和解决方案
- cocos2d-x3.1.1 step by step 学习笔记2 cocos中的字符串
- cocos2d-x JS 字符串
- [寒江孤叶丶的Cocos2d-x之旅_15]Cocos2d-x LUA脚本中对字符串进行Base64加解密/编解码 (LUA Base64 加解密/编解码)
- cocos2d-x 对传入的字符串进行解析分割 解析
- cocos2d-x 自定义分割字符串的封装类
- Cocos2d-x计算字符串Size方法
- Cocos2D-x游戏开发之二十四:自定义字符串分割类
- 【学习笔记】cocos2d 如何绘制一个字符串
- [Cocos2d-x源码] 一些整数/字符串转换常用的宏定义
- [寒江孤叶丶的Cocos2d-x之旅_36]用LUA实现UTF8的字符串基本操作 UTF8字符串长度,UTF8字符串剪裁等
- [寒江孤叶丶的Cocos2d-x之旅_01]浅谈多平台支持的格式化字符串(字符串连接)
- [寒江孤叶丶的Cocos2d-x之旅_36]用LUA实现UTF8的字符串基本操作 UTF8字符串长度,UTF8字符串剪裁等
- cocos2d 字符串库打包问题2.X和3.X
- cocos2d-x类型转换(CCstring int string char UTF-8互转)以及字符串详解
- Cocos2d-x中的字符串
- cocos2d-x 整形转换成字符串的解决方法
- cocos2d-x 对字符串的传值操作