Cocos2dx中英文混合字符串截取

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

一、定義頭文件

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


二、實現文件

  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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章