Cocos2d-x中的字符串
来源:互联网 发布:美工30后就不能做了吗 编辑:程序博客网 时间:2024/05/16 18:56
在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对象。
std::string name = "tony";std::string name = std::string("tony");
我们不需要使用指针,也不需要关心内存释放问题,在作用域超出之后std::string对象别释放。我们可以通过下面的语句把std::string转化为const char*类型。
const char* cstring = name.c_str();
我们可以使用std::string指针类型,但是要配合使用new关键字开辟内存空间,然后不再使用的时候要通过delete释放内存。
std::string* name =newstd::string("tony"); … … delete name;
使用std::string指针对象时候,我们可以通过下面的代码转化为const char*类型。
const char* cstring = name->c_str();
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函数如下:
static__String * create (const std::string &str)static__String * createWithFormat (const char *format,...)
使用create函数的实例代码如下:
__String* name= __String::create("Hi,Tony");int num=123;__String* ns = __String::createWithFormat("%d",num);
cocos2d::__String还提供了一些数据类型之间的转换函数。例如:cocos2d::__String转换为const char*类型,这种转换用的比较多的,示例代码如下:
__String* name= __String::create("Hi,Tony");const char *cstring=name->getCString();
const char*转换为cocos2d::__String类型,示例代码如下:
const char* cstring = "Hi,Tony";__String*ns=__String::createWithFormat("%s",cstring);
std::string转换为cocos2d::__String类型,示例代码如下:
std::string string = "Hi,Tony"; __String*ns=__String::createWithFormat("%s",string.c_str());
cocos2d::__String转换为int类型,示例代码如下:
int num = 123;__String* ns =__String::createWithFormat("%d",num);int num2 = ns->intValue();
还有很多函数我们会在以后的学习再给大家介绍。
《Cocos2d-x实战 C++卷》现已上线,各大商店均已开售:
京东:http://item.jd.com/11584534.html
亚马逊:http://www.amazon.cn/Cocos2d-x%E5%AE%9E%E6%88%98-C-%E5%8D%B7-%E5%85%B3%E4%B8%9C%E5%8D%87/dp/B00PTYWTLU
当当:http://product.dangdang.com/23606265.html
互动出版网:http://product.china-pub.com/3770734
《Cocos2d-x实战 C++卷》源码及样章下载地址:
源码下载地址:http://51work6.com/forum.php?mod=viewthread&tid=1155&extra=page%3D1
样章下载地址:http://51work6.com/forum.php?mod=viewthread&tid=1157&extra=page%3D1
- Cocos2d-x中的字符串
- Cocos2d-x中的字符串
- Cocos2d-x中的字符串
- cocos2d-x 分割字符串
- cocos2d-x中的随机数
- Cocos2d-x 中的字体
- cocos2d-x中的随机数
- cocos2d-x中的CCArray
- cocos2d-x 中的 CCSprite
- cocos2d-x中的CCArray
- cocos2d-x 中的CCTileMap
- cocos2d-x中的坐标系
- cocos2d-x中的坐标系
- cocos2d-x中的curl
- Cocos2d-X中的坐标系
- Cocos2d-X中的字体
- Cocos2d-X中的CCSprite
- Cocos2d-X中的CCEditBox
- 美柚“姨妈假”上头条,App事件营销怎么做
- zlg9518S spi驱动
- VC打印控制
- mysql的数据库引擎
- 如何定义糗事百科的盈利模式?或许是公益事业?
- Cocos2d-x中的字符串
- 婆媳矛盾引发小夫妻动手 女子抄尖刀刺死丈夫
- IOS之同步请求、异步请求、GET请求、POST请求
- vc++使用内存映射
- 电阻阻值标准表
- org.apache.commons.collections.CollectionUtils
- OpenGL在Windows环境下的配置
- js獲取時間方法描述
- ASP.NET的错误处理机制