cocos2d-x实战 C++卷 学习笔记--第4章 字符串 __String类

前言:

《cocos2d-x实战C++卷》学习笔记。(cocos2d-x 是3.0版本)

介绍 cocos2d-x 通用的字符串类  __String .

使用cocos2d::__String

cocos2d::__String 是 Cocos2d-x通用的字符串类,它的设计模拟了Objective-c的NSString类,这由于Cocos2d-x源自于Cocos2d-iphone, cocos2d::__String也是 基于 Unicode 双字节 编码。
主要用它的静态方法来创建字符串。因为 create 静态函数创建的对象,不需要我们管理内存释放

 

 1     /// cocos2d::__String 转 const char* 
 2     __String* name = __String::create("Hi, Master Oogway.");
 3     const char* cstring1 = name->getCString();
 4     
 5     //// const char* 转 __String
 6     const char* cstring2 = "Hi, Master Oogway";
 7     __String* ns1 = __String::createWithFormat("%s", cstring2);
 8 
 9     /// std::string 转 cocos2d::__String 
10     std::string str = "Hi, string Oogway";
11     __String* ns2 = __String::createWithFormat("%s", str.c_str());
12 
13     /// cocos2d::__String 转为 int
14     int num = 123;
15     __String* ns3 = __String::createWithFormat("%d", num);
16     int num2 = ns3->intValue();
17 
18     __String* strLog = __String::create("hello, 晚上好! abcdefg");
19     log("%s", strLog->getCString());   输入的 日志文件有乱码,下一篇文章讲乱码问题.

 

转载于:https://www.cnblogs.com/MasterOogway/p/5805082.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值