Introduction
CCString is inherited from the CCObject class. CCObject exist primarily as an automatic memory management object. CCString provided a series of interface such as create, convert etc..
Useful Methods
Creation :
1 2 /** create a string with std string, you can also pass a c string pointer because the default constructor of std::string can access a c string pointer. 3 4 * @return A CCString pointer which is an autorelease object pointer, 5 6 * it means that you needn't do a release operation unless you retain it. 7 8 */ 910 static CCString* create(const std::string& str);1112 /** create a string with format, it's similar with the c function 'sprintf', the default buffer size is (1024*100) bytes,1314 * if you want to change it, you should modify the kMaxStringLen macro in CCString.cpp file.1516 * @return A CCString pointer which is an autorelease object pointer,1718 * it means that you needn't do a release operation unless you retain it.1920 */ 2122 static CCString* createWithFormat(const char* format, …);2324 /** create a string with binary data 2526 * @return A CCString pointer which is an autorelease object pointer,2728 * it means that you needn't do a release operation unless you retain it.2930 */3132 static CCString* createWithData(const unsigned char* pData, unsigned long nLen);3334 /** create a string with a file, 3536 * @return A CCString pointer which is an autorelease object pointer,3738 * it means that you needn't do a release operation unless you retain it.3940 */4142 static CCString* createWithContentsOfFile(const char* pszFileName);43
Conversion
The CCString is also allows a CCString variable converts to a variable of another type:
1 2 /** convert to int value */ 3 4 int intValue() const; 5 6 /** convert to unsigned int value */ 7 8 unsigned int uintValue() const; 910 /** convert to float value */1112 float floatValue() const;1314 /** convert to double value */1516 double doubleValue() const;1718 /** convert to bool value */1920 bool boolValue() const;21
Useful Macros
12#define CCStringMake(str) CCString::create(str)34#define ccs CCStringMake5
These macros can construct an autorelease CCString object more easily, e.g. If we want to new a lot of CCString object, and add them into a CCArray,the following codes will achieve that and make your code more simple.
1 2 CCArray *stringArray = CCArray::create( 3 4 ccs("Hello"), 5 6 ccs("Variable"), 7 8 ccs("Size"), 910 ccs("!"),1112 NULL); come from: http://www.cocos2d-x.org/projects/cocos2d-x/wiki/CCString