zoukankan      html  css  js  c++  java
  • Cocos2d-x之String

    |   版权声明:本文为博主原创文章,未经博主允许不得转载。

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

    API中__String的主要函数:

     1 bool initWithFormat(const char* format, ...) CC_FORMAT_PRINTF(2, 3);
     2     初始化与格式的字符串,它与c函数“的sprintf”相似
     3 int intValue() const;
     4     转换成int值
     5 unsigned int uintValue() const;
     6     转换为unsigned  int值
     7 float floatValue() const;
     8     转换为float值
     9 double doubleValue() const;
    10     转换为double值
    11 bool boolValue() const;
    12     转换为布尔值
    13 const char* getCString() const;
    14     得到C字符串
    15 int length() const;
    16     获得字符串的长度
    17 int compare(const char *) const;  
    18     相比C字符串
    19 void append(const std::string& str);
    20     在当前值的末尾附加其他字符
    21 void appendWithFormat(const char* format, ...); 
    22     追加(W /格式)在当前值的末尾附加字符
    23 virtual bool isEqual(const Ref* pObject);    
    24     重写函数
    25 static __String* create(const std::string& str);
    26     创建字符串,可以通过一个C字符串指针,因为std::String的默认构造函数可以访问C字符串的指针。返回:一个字符串指针,它是一个自动释放对象的指针,这意味着你,除非你保留它没有必要做了释放操作。
    27 static __String* createWithFormat(const char* format, ...) CC_FORMAT_PRINTF(1, 2);
    28     创建具有格式的字符串,它与c函数'sprintf的'类似,默认的缓冲区大小为(1024* 100)个字节,如果你想改变它,你应该修改String.cpp文件kMax__StringLen宏。返回:一个字符串指针,它是一个自动释放对象指针,*它意味着你,除非你保留它没有必要做了释放操作。
    29 static __String* createWithData(const unsigned char* pData, size_t nLen);
    30     创建具有二进制数据的字符串, 返回:一个字符串指针,它是一个自动释放对象的指针,这意味着你,除非你保留它没有必要做了释放操作  
    31 static __String* createWithContentsOfFile(const std::string& filename);  
    32     创建一个字符串的文件,返回:一个字符串指针,它是一个自动释放对象的指针,这意味着,除非你保留它没有必要做了释放操作。
    33 void appendWithFormat(const char* format, ...);
    34     拆分一个字符串

    实例:

    .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());
    }
    

  • 相关阅读:
    【转】【SEE】基于SSE指令集的程序设计简介
    【转】【Asp.Net】asp.net服务器控件创建
    ControlTemplate in WPF ——ScrollBar
    ControlTemplate in WPF —— Menu
    ControlTemplate in WPF —— Expander
    ControlTemplate in WPF —— TreeView
    ControlTemplate in WPF —— ListBox
    ControlTemplate in WPF —— ComboBox
    ControlTemplate in WPF —— TextBox
    ControlTemplate in WPF —— RadioButton
  • 原文地址:https://www.cnblogs.com/geore/p/5799147.html
Copyright © 2011-2022 走看看