COCOS对字符串处理方式整理

判断字符是英文还是汉字

/*
brief 判断字符是英文还是汉字
param ch 字符(字节)
return true:是英文;false:是中文
*/
static bool isEnglishChar(char ch)
{
    /*汉字的三个字节(有些编码格式是两个字节)的最高为都为1,这里采用判断最高位的方法
    将ch字节进行移位运算,右移8位,这样,如果移位后是0,
    则说明原来的字节最高位为0,不是1那么也就不是汉字的一个字节
    */
    if (~(ch >> 8) == 0)
    {
        return false;  //代表不是汉字
    }
    return true;
}

判断字符是英文还是汉字

//是否是汉字
static bool IsChinese(std::string strBuffer)
{
    if (strBuffer == "")
    {
        return false;
    }
    for (int i = 0; i < strBuffer.length()-1; i++)
    {
        if (!isEnglishChar(strBuffer.at(i)))
        {
            return true;
        }
    }
    return false;
}

获取剪切排除emoji表情的字符串

//获取剪切排除emoji表情的字符串
static std::string GetStringByCutEmoji(std::string strValue)
{
    int size = strValue.size();
    std::string result = "";
    if (size > 0)
    {
        for (int i = 0; i < size; i++)
        {
            //是否是英文
            if (isEnglishChar(strValue[i]))
            {
                result.push_back(strValue[i]);
            }
            else
            {
                if ((unsigned)(strValue[i] & 0xf0) == 0xe0)
                {
                    for (int j = 0; j < CHINESE_CHAR_LENGTH_UTF8; ++j)
                    {
                        result.push_back(strValue[i + j]);
                    }
                    i += CHINESE_CHAR_LENGTH_UTF8 - 1;
                }
            }
        }
    }
    return result.c_str();
}

截取字符串

/*
brief 截取字符串
param start 起始下标,从1开始
param end   结束下标
param isNeedPoint 是否需要在末尾添加“...”
return 截取之后的字符串
*/
static std::string CutStringUtil(std::string util,int start, int end, bool isNeedPoint)
{
    //CCLOG("util = %d", util.length());
    util = GetStringByCutEmoji(util);
    if ( util.length() <= 0 || start >= end )
        return util;

    std::vector<std::string> _result;
    int i = 0;
    while (i < util.length())
    {
        if (!isEnglishChar(util.at(i)))
        {
            _result.push_back(util.substr(i, CHINESE_CHAR_LENGTH_UTF8));  // 一个汉字三个字节
            i = i + CHINESE_CHAR_LENGTH_UTF8;
        }
        else
        {
            _result.push_back(util.substr(i, 1));  // 一个英文一个字节
            i = i + 1;
        }
    }

    if (start >= 1 && _result.size() <= start)return util;

    // 容错处理,如果end大于字符串长度,则舍弃多余部分
    end = _result.size() >= end ? end : _result.size();
    std::string temp = "";
    //直接从_result里取即可
    int max = end * 2;
    int curValue = 0;
    int enIdx = 0;
    for (int i = start; i < end; i++)
    {
        if (i > _result.size() - 1 || curValue >= max)
        {
            break;
        }
        temp += _result[i];

        //CCLOG("_result[%d] = %s", i, _result[i].c_str());
        if (!IsChinese(_result[i].c_str()))
        {
            curValue++;
            if (++enIdx % 2 == 0)
                end++;
        }
        else
        {
            curValue += 2;
        }
    }

    // 如果字符串太长,在末尾添加“...”
    if (isNeedPoint)
    {
        if (_result.size() > end)
        {
            temp += "...";
        }
    }
    //CCLOG("temp = %s", temp.c_str());
    return temp;
}

//判断是不是整形
static bool IsINT(const char *str)
{
    for (int i = 0; i < strlen(str); i++)
    if (str[i] < '0' || str[i] > '9')
        return false;
    return true;
}

猜你喜欢

转载自blog.csdn.net/zhengjuqiang/article/details/80078195
今日推荐