现在的位置: 首页 > 综合 > 正文

cocos2d-x节点(CCString.h)API

2014年02月25日 ⁄ 综合 ⁄ 共 2842字 ⁄ 字号 评论关闭

本文来自http://blog.csdn.net/runaying ,引用必须注明出处!

cocos2d-x节点(CCString.h)API

温馨提醒:为了大家能更好学习,强烈推荐大家看看本人的这篇博客 Cocos2d-X权威指南笔记

CCString 很强大,各种数据类型的转换(把字符串转化为 doublefloatinteger......)。字符串的分割和比较

///\cocos2d-x-3.0alpha0\cocos2dx\cocoa
//CCString 很强大,各种数据类型的转换(把字符串转化为 double、float、integer......)。字符串的分割和比较

#ifndef __CCSTRING_H__
#define __CCSTRING_H__

#if (CC_TARGET_PLATFORM == CC_PLATFORM_BLACKBERRY)
#include <string.h>
#endif

#include <stdarg.h>
#include <string>
#include <functional>
#include "CCObject.h"

NS_CC_BEGIN

/**
 * @addtogroup data_structures
 * @{
 */

class CC_DLL String : public Object, public Clonable
{
public:
    /**
     * @js NA
     * @lua NA
     */
    String();
    /**
     * @js NA
     * @lua NA
     */
    String(const char* str);
    /**
     * @js NA
     * @lua NA
     */
    String(const std::string& str);
    /**
     * @js NA
     * @lua NA
     */
    String(const String& str);
    /**
     * @js NA
     * @lua NA
     */
    virtual ~String();
    
    /* 重载(override)赋值运算符
     * @js NA
     * @lua NA
     */
    String& operator= (const String& other);

    /** 使用 format 初始化字符串, 他类似于 C 函数的 'sprintf' 
     * @js NA
     * @lua NA
     */
    bool initWithFormat(const char* format, ...) CC_FORMAT_PRINTF(2, 3);

    /** 转换为int值
     * @js NA
     */
    int intValue() const;

    /**转换为 unsigned 值
     * @js NA
     */
    unsigned int uintValue() const;

    /** 转换为 float 值 
     * @js NA
     */
    float floatValue() const;

    /** 转换为 double 值 
     * @js NA
     */
    double doubleValue() const;

    /** 转换为 bool 值 
     * @js NA
     */
    bool boolValue() const;

    /** get the C string 
     * @js NA
     */
    const char* getCString() const;

    /** get  string 的长度
     * @js NA
     */
    unsigned int length() const;

    /** C字符串比较
     * @js NA
     */
    int compare(const char *) const;

    /** 当前值的末尾追加额外的字符 
     * @js NA
     * @lua NA
     */
    void append(const std::string& str);

    /** 当前值的末尾追加(w/ format)额外的字符
     * @js NA
     * @lua NA
     */
    void appendWithFormat(const char* format, ...);

    /** 将字符串分割
     * @js NA
     * @lua NA
     */
    Array* componentsSeparatedByString(const char *delimiter);
    
    /* override functions 
     * @js NA
     */
    virtual bool isEqual(const Object* pObject);

    /** 使用 std string 创建一个字符串, 你也可以使用一个  c string 指针,因为 std::string 的默认构造函数可以接受一个 c string 指针. 
     *  @return 一个字符串指针,这是一个自动释放的对象指针,
     *          这意味着你不需要 release 操作除非你 retain 他了.
     * @js NA
     */
    static String* create(const std::string& str);

    /**  使用 format 创建字符串, 他类似于 C 函数的 'sprintf' , 默认的缓冲大小是 (1024*100) bytes,
     *  如果你想改变它,你应该在 String.cpp 文件里面修改 kMaxStringLen 宏.
     *  @return  一个字符串指针,这是一个自动释放的对象指针,
     *          这意味着你不需要 release 操作除非你 retain 他了.
     * @js NA
     */
    static String* createWithFormat(const char* format, ...) CC_FORMAT_PRINTF(1, 2);

    /** create 一个 binary data 创建字符串
     *  @return  一个字符串指针,这是一个自动释放的对象指针,
     *          这意味着你不需要 release 操作除非你 retain 他了.
     * @js NA
     */
    static String* createWithData(const unsigned char* pData, unsigned long nLen);

    /** create 一个文件创建字符串
     *  @return  一个字符串指针,这是一个自动释放的对象指针,
     *          这意味着你不需要 release 操作除非你 retain 他了.
     * @js NA
     */
    static String* createWithContentsOfFile(const char* filename);
    /**
     * @js NA
     * @lua NA
     */
    virtual void acceptVisitor(DataVisitor &visitor);
    /**
     * @js NA
     * @lua NA
     */
    virtual String* clone() const;
    
private:

    /** 仅供内部使用 */
    bool initWithFormatAndValist(const char* format, va_list ap);

public:
    std::string _string;
};

struct StringCompare : public std::binary_function<String *, String *, bool> {
    public:
        bool operator() (String * a, String * b) const {
            return strcmp(a->getCString(), b->getCString()) < 0;
        }
};

#define StringMake(str) String::create(str)
#define ccs               StringMake

// end of data_structure group
/// @}

NS_CC_END

#endif //__CCSTRING_H__

抱歉!评论已关闭.