Cocos2d-x之String
| 版权声明:本文为博主原创文章,未经博主允许不得转载。
在Cocos2d-x中能够使用的字符串constchar*、std::string和cocos2d::__String等,其中const char*和std::string是C++风格的字符串,它封装了const char*。cocos2d::__String才是Cocos2d-x引擎提供的字符串类,这些字符串都可以互相转换。
API中__String的主要函数:
bool initWithFormat(const char* format, ...) CC_FORMAT_PRINTF(, );
初始化与格式的字符串,它与c函数“的sprintf”相似
int intValue() const;
转换成int值
unsigned int uintValue() const;
转换为unsigned int值
float floatValue() const;
转换为float值
double doubleValue() const;
转换为double值
bool boolValue() const;
转换为布尔值
const char* getCString() const;
得到C字符串
int length() const;
获得字符串的长度
int compare(const char *) const;
相比C字符串
void append(const std::string& str);
在当前值的末尾附加其他字符
void appendWithFormat(const char* format, ...);
追加(W /格式)在当前值的末尾附加字符
virtual bool isEqual(const Ref* pObject);
重写函数
static __String* create(const std::string& str);
创建字符串,可以通过一个C字符串指针,因为std::String的默认构造函数可以访问C字符串的指针。返回:一个字符串指针,它是一个自动释放对象的指针,这意味着你,除非你保留它没有必要做了释放操作。
static __String* createWithFormat(const char* format, ...) CC_FORMAT_PRINTF(, );
创建具有格式的字符串,它与c函数'sprintf的'类似,默认的缓冲区大小为(* )个字节,如果你想改变它,你应该修改String.cpp文件kMax__StringLen宏。返回:一个字符串指针,它是一个自动释放对象指针,*它意味着你,除非你保留它没有必要做了释放操作。
static __String* createWithData(const unsigned char* pData, size_t nLen);
创建具有二进制数据的字符串, 返回:一个字符串指针,它是一个自动释放对象的指针,这意味着你,除非你保留它没有必要做了释放操作
static __String* createWithContentsOfFile(const std::string& filename);
创建一个字符串的文件,返回:一个字符串指针,它是一个自动释放对象的指针,这意味着,除非你保留它没有必要做了释放操作。
void appendWithFormat(const char* format, ...);
拆分一个字符串
实例:
.h files #ifndef _STRINGTEST_SCENE_H_
#define _STRINGTEST_SCENE_H_
#include "cocos2d.h"
class stringTest : public cocos2d::Layer
{
private:
public:
static cocos2d::Scene* createScene();
virtual bool init();
void string_test();
CREATE_FUNC(stringTest);
};
#endif // _STRINGTEST_SCENE_H_ .cpp files #include "StringTest.h"
USING_NS_CC;
Scene* stringTest::createScene()
{ // 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = stringTest::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool stringTest::init()
{
// 1. super init first
if (!Layer::init())
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24);
label->setPosition(Vec2(origin.x + visibleSize.width / 2,
origin.y + visibleSize.height - label->getContentSize().height));
this->addChild(label, 1);
auto sprite = Sprite::create("HelloWorld.png");
sprite->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y));
this->addChild(sprite, 0);
string_test();
return true;
}
void stringTest::string_test()
{ //1. 创建一个字符串
//首先创建一个字符串ch
String* ch = String::create("I love China!");
//再将字符串ch转换成c风格的字符串,最后将它打印出来
//getCString();函数是将字符串转换成一个c风格的字符串
CCLOG("%s", ch->getCString()); //2. 获取字符串的长度
int len = ch->length();
CCLOG("Length of string ch : length = %d", len); //3. 字符串的连接
String* name = String::create("Peter");
int age = 45;
String* linkString = String::createWithFormat("He name is %s,the age is %d", name->getCString(), age);
CCLOG("%s", linkString->getCString()); //4. 类型的转换
String* intStatus = String::create("456");
//转换成整形
int number = intStatus->intValue();
CCLOG("%d", number); //5. 追加
String* str1 = String::create("Nice");
//String* str2 = String::create(" day!");
//append();函数是一个在字符串末尾追加字符的函数
str1->append(" day!");
CCLOG("%s", str1->getCString());
}

Cocos2d-x之String的更多相关文章
- cocos2dx骨骼动画Armature源码分析(三)
代码目录结构 cocos2dx里骨骼动画代码在cocos -> editor-support -> cocostudio文件夹中,win下通过筛选器,文件结构如下.(mac下没有分,是整个 ...
- Cocos2d-x 3.0 Json用法 Cocos2d-x xml解析
Cocos2d-x 3.0 加入了rapidjson库用于json解析.位于external/json下. rapidjson 项目地址:http://code.google.com/p/rapidj ...
- 【转】cocos2d-x Lua
Call custom c++ from Lua cocos2d-x lua binds c++ class, class functions ,enum and some global functi ...
- Cocos2d-x数据持久化-修改数据
修改数据时,涉及的SQL语句有insert.update和delete语句,这3个SQL语句都可以带参数.修改数据的具体步骤如下所示.(1) 使用sqlite3_open函数打开数据库.(2) 使用s ...
- cocos2d-x实战 C++卷 学习笔记--第4章 字符串 __String类
前言: <cocos2d-x实战C++卷>学习笔记.(cocos2d-x 是3.0版本) 介绍 cocos2d-x 通用的字符串类 __String . 使用cocos2d::__Str ...
- cocos2d-x 读取 json 文件并用 jsoncpp 做解析
一码胜万言(请看注释) CclUtil.h // // CclUtil.h // PracticeDemo // // Created by kodeyang on 8/1/13. // // #if ...
- 【Cocos2d-x游戏引擎开发笔记(25)】XML解析
原创文章,转载请注明出处:http://blog.csdn.net/zhy_cheng/article/details/9128819 XML是一种非常重要的文件格式,由于C++对XML的支持非常完善 ...
- Cocos2d-x数据持久-变更数据
当数据变化,参与SQL报表insert.update和delete声明.这项3个月SQL语句可以带参数. 详细过程的数据,例如,下面的变化看出.(1) 采用sqlite3_open开放式数据库功能.( ...
- address2line 定位 Android c++奔溃位置
Android调用c++出现奔溃,崩溃信息为如下: 10-11 15:15:13.541 D/AudioMTKStreamOut( 139): write(), buffer = 0x42bd9390 ...
- cocos2d-js 3.0 RC0 手动绑定 C++调用js,js调用C++ jsbinding
参考:http://www.tairan.com/archives/4902 参考文章是2.x版本的,对于3.0也许不合适了,没有深究. 代码:https://github.com/kenkozhen ...
随机推荐
- python学习三十三天函数匿名函数lambda用法
python函数匿名函数lambda用法,是在多行语句转换一行语句,有点像三元运算符,只可以表示一些简单运算的,lambda做一些复杂的运算不太可能.分别对比普通函数和匿名函数的区别 1,普通的函数用 ...
- Springboot-技术专区-war包部署在Tomcat上并修改默认端口
springboot项目内置Tomcat,直接打成jar包在dos下运行即可,但有时我们需要用war包以非内嵌Tomcat的方式来部署,以下是本人的实际经验 1.首先需要修改pom.xml文件 < ...
- Oracle 汉字占用字节数
在oracle中一个字符特别是中文字符占几个字节是与字符集有关的. 比如GBK,汉字就会占两个字节,英文1个:如果是UTF-8,汉字一般占3个字节,英文还是1个.但是一般情况下,我们都认为是 ...
- Source Insight symbol not found
使用SourceInsight查看源代码时,发现点击查看相关类型时,无法关联到其代码,出现 symbol not found, 然而明明在我的头文件有定义的 网上查了一下主要是因为新建工程导入文件后, ...
- tornado后台小框架
import tornado.ioloop import tornado.web """使用get方法提交过来数据就是用get方法,使用post执行post方法这个框架的 ...
- HR面试总结
求职面试HR最欣赏的自我介绍 2015-02-25 来源:www.cnrencai.com 浏览:391 明明很有能力的你,在面试中却不能发挥出色?来看看是不是自我介绍环节出了问题. 回答面试题目 ...
- 攻防世界--Hello, CTF
测试文件地址:https://www.lanzous.com/i5ot1yd 使用IDA1打开 打开之后,这个字符串和第一题的有些类似,拿去转换一下,Flag就得到了 CrackMeJustForFu ...
- 树的计数 Prüfer编码与Cayley公式 学习笔记
最近学习了Prüfer编码与Cayley公式,这两个强力的工具一般用于解决树的计数问题.现在博主只能学到浅层的内容,只会用不会证明. 推荐博客:https://blog.csdn.net/moreja ...
- [CodeForces - 1225E]Rock Is Push 【dp】【前缀和】
[CodeForces - 1225E]Rock Is Push [dp][前缀和] 标签:题解 codeforces题解 dp 前缀和 题目描述 Time limit 2000 ms Memory ...
- 设置Oracle PL/SQL时间显示格式NLS_TIMESTAMP_FORMAT
Oracle中TIMESTAMP时间的显示格式 Oracle数据库的时间字段我们通常是使用timestamp 格式,在未做设置前, 查询出来的数据类似于“27-1月 -08 12.04.35.87 ...