Outdated Docs > CCString

CCString

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 :

    /** 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. 

     * @return A CCString pointer which is an autorelease object pointer,

     * it means that you needn't do a release operation unless you retain it.

     */

    static CCString* create(const std::string& str);

    /** create a string with format, it's similar with the c function 'sprintf', the default buffer size is (1024*100) bytes,

     * if you want to change it, you should modify the kMaxStringLen macro in CCString.cpp file.

     * @return A CCString pointer which is an autorelease object pointer,

     * it means that you needn't do a release operation unless you retain it.

     */ 

    static CCString* createWithFormat(const char* format, …);

    /** create a string with binary data 

     * @return A CCString pointer which is an autorelease object pointer,

     * it means that you needn't do a release operation unless you retain it.

     */

    static CCString* createWithData(const unsigned char* pData, unsigned long nLen);

    /** create a string with a file, 

     * @return A CCString pointer which is an autorelease object pointer,

     * it means that you needn't do a release operation unless you retain it.

     */

    static CCString* createWithContentsOfFile(const char* pszFileName);

Conversion

The CCString is also allows a CCString variable converts to a variable of another type:

    /** convert to int value */

    int intValue() const;



    /** convert to unsigned int value */

    unsigned int uintValue() const;



    /** convert to float value */

    float floatValue() const;



    /** convert to double value */

    double doubleValue() const;



    /** convert to bool value */

    bool boolValue() const;

Useful Macros

#define CCStringMake(str) CCString::create(str)

#define ccs CCStringMake

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.

        CCArray *stringArray = CCArray::create(

            ccs("Hello"),

            ccs("Variable"),

            ccs("Size"),

            ccs("!"),

            NULL);

Sign up for our newsletter to keep up with the latest developments, releases and updates for Cocos2d-x.