Cocos2dx中英文混合字符串截取

PS:参考博文:http://blog.sina.com.cn/s/blog_939c22bc01019coo.html

一、定义头文件

[cpp]  view plain  copy
  1. #ifndef XCaseStringUtil_hpp  
  2. #define XCaseStringUtil_hpp  
  3.   
  4.   
  5. #include <stdio.h>  
  6. #include "cocos2d.h"  
  7.   
  8.   
  9. USING_NS_CC;  
  10. using namespace std;  
  11.   
  12. class XCaseStringUtil : public Ref{  
  13. public:  
  14.     XCaseStringUtil();  
  15.     ~XCaseStringUtil();  
  16.     static XCaseStringUtil* create(std::string str);  
  17.   
  18. private:  
  19.     virtual bool init(std::string str);  
  20.     // 解析字符串  
  21.     void parseString();  
  22.   
  23. public:  
  24.     // 判断是中文还是英文  
  25.     bool isEnglishChar(char ch);  
  26.     // 计算字符串的长度,中文英文都算作一个字节  
  27.     int getLength();  
  28.     // 截取字符串,返回截取之后的字符串  
  29.     std::string sub(int start, int end = -1, bool isNeedPoint = false);  
  30.   
  31. private:  
  32.     std::string _target;    // 存储传入的字符串  
  33.     vector<string> _result;   // 存储解析之后的字符串  
  34. };  
  35. #endif /* XCaseStringUtil_hpp */  


二、实现文件

[cpp]  view plain  copy
  1. #include <iostream>  
  2. #include <string>  
  3. #include <cstdio>  
  4. #include <vector>  
  5. #include <typeinfo>  
  6. #include "XCaseStringUtil.h"  
  7.   
  8. #define CHINESE_CHAR_LENGTH_UTF8 3  // 根据编码格式定义一个中文字符占几个字节(默认为UTF-8,三个字节)  
  9.   
  10.   
  11. XCaseStringUtil::XCaseStringUtil():  
  12. _target("")  
  13. {  
  14.   
  15.   
  16. }  
  17.   
  18.   
  19. XCaseStringUtil::~XCaseStringUtil()  
  20. {  
  21.   
  22.   
  23. }  
  24.   
  25.   
  26. XCaseStringUtil* XCaseStringUtil::create(std::string str)  
  27. {  
  28.     auto pRet = new XCaseStringUtil();  
  29.     if (pRet && pRet->init(str))  
  30.     {  
  31.         pRet->autorelease();  
  32.         return pRet;  
  33.     }  
  34.     CC_SAFE_RELEASE_NULL(pRet);  
  35.     return nullptr;  
  36. }  
  37.   
  38.   
  39. bool XCaseStringUtil::init(std::string str)  
  40. {  
  41.     bool isInit = false;  
  42.     do  
  43.     {  
  44.         _target = str;  
  45.         _result.clear();  
  46.   
  47.   
  48.         parseString();  
  49.         isInit = true;  
  50.     } while (0);  
  51.     return isInit;  
  52. }  
  53.   
  54.   
  55. /* 
  56. brief 解析字符串,将中英文字符都处理成一个“字节” 
  57. */  
  58. void XCaseStringUtil::parseString()  
  59. {  
  60.     int i = 0;  
  61.     while (i < _target.length())  
  62.     {  
  63.         if (!isEnglishChar(_target.at(i)))  
  64.         {  
  65.             _result.push_back(_target.substr(i, CHINESE_CHAR_LENGTH_UTF8));  // 一个汉字三个字节  
  66.             i = i + CHINESE_CHAR_LENGTH_UTF8;  
  67.   
  68.   
  69.         }  
  70.         else  
  71.         {  
  72.             _result.push_back(_target.substr(i, 1));  // 一个英文一个字节  
  73.             i = i + 1;  
  74.         }  
  75.     }  
  76. }  
  77.   
  78.   
  79. /* 
  80. brief 判断字符是英文还是汉字 
  81. param ch 字符(字节) 
  82. return true:是英文;false:是中文 
  83. */  
  84. bool XCaseStringUtil::isEnglishChar(char ch)  
  85. {  
  86.   
  87.   
  88.     /*汉字的三个字节(有些编码格式是两个字节)的最高为都为1,这里采用判断最高位的方法 
  89.     将ch字节进行移位运算,右移8位,这样,如果移位后是0, 
  90.     则说明原来的字节最高位为0,不是1那么也就不是汉字的一个字节 
  91.     */  
  92.     if (~(ch >> 8) == 0)  
  93.     {  
  94.         return false;  //代表不是汉字    此处原文错误 应为是汉字
  95.     }  
  96.   
  97.   
  98.     return true;  
  99. }  
  100.   
  101.   
  102. /* 
  103. brief 获取字符串长度 
  104. param str   目标字符串 
  105. return 字符串的长度 
  106. */  
  107. int XCaseStringUtil::getLength()  
  108. {  
  109.     return _result.size();  // 返回字符串长度  
  110. }  
  111.   
  112.   
  113. /* 
  114. brief 截取字符串 
  115. param start 起始下标,从1开始 
  116. param end   结束下标 
  117. param isNeedPoint 是否需要在末尾添加“...” 
  118. return 截取之后的字符串 
  119. */  
  120. string XCaseStringUtil::sub(int start, int end, bool isNeedPoint)  
  121. {  
  122.     CCASSERT(getLength() != 0, "string is null");  
  123.     CCASSERT(start >= 1 && getLength() >= start, "start is wrong");  
  124.     CCASSERT(start <= end, "end is wrong");  
  125.   
  126.   
  127.     // 容错处理,如果end大于字符串长度,则舍弃多余部分  
  128.     end = getLength() >= end ? end : getLength();  
  129.   
  130.   
  131.     string temp = "";  
  132.     //直接从_result里取即可  
  133.     for (int i = start; i <= end; i++)  
  134.     {  
  135.         temp += _result[i - 1];  
  136.     }  
  137.   
  138.   
  139.     // 如果字符串太长,在末尾添加“...”  
  140.     if (isNeedPoint)  
  141.     {  
  142.         if (getLength() > end)  
  143.         {  
  144.             temp += "...";  
  145.         }  
  146.     }  
  147.     return temp;  
  148. }  
转载地址:https://blog.csdn.net/u013058216/article/details/53289731
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值