PS:參考博文:http://blog.sina.com.cn/s/blog_939c22bc01019coo.html
一、定義頭文件
- #ifndef XCaseStringUtil_hpp
- #define XCaseStringUtil_hpp
- #include <stdio.h>
- #include "cocos2d.h"
- USING_NS_CC;
- using namespace std;
- class XCaseStringUtil : public Ref{
- public:
- XCaseStringUtil();
- ~XCaseStringUtil();
- static XCaseStringUtil* create(std::string str);
- private:
- virtual bool init(std::string str);
- // 解析字符串
- void parseString();
- public:
- // 判斷是中文還是英文
- bool isEnglishChar(char ch);
- // 計算字符串的長度,中文英文都算作一個字節
- int getLength();
- // 截取字符串,返回截取之後的字符串
- std::string sub(int start, int end = -1, bool isNeedPoint = false);
- private:
- std::string _target; // 存儲傳入的字符串
- vector<string> _result; // 存儲解析之後的字符串
- };
- #endif /* XCaseStringUtil_hpp */
二、實現文件
- #include <iostream>
- #include <string>
- #include <cstdio>
- #include <vector>
- #include <typeinfo>
- #include "XCaseStringUtil.h"
- #define CHINESE_CHAR_LENGTH_UTF8 3 // 根據編碼格式定義一箇中文字符佔幾個字節(默認爲UTF-8,三個字節)
- XCaseStringUtil::XCaseStringUtil():
- _target("")
- {
- }
- XCaseStringUtil::~XCaseStringUtil()
- {
- }
- XCaseStringUtil* XCaseStringUtil::create(std::string str)
- {
- auto pRet = new XCaseStringUtil();
- if (pRet && pRet->init(str))
- {
- pRet->autorelease();
- return pRet;
- }
- CC_SAFE_RELEASE_NULL(pRet);
- return nullptr;
- }
- bool XCaseStringUtil::init(std::string str)
- {
- bool isInit = false;
- do
- {
- _target = str;
- _result.clear();
- parseString();
- isInit = true;
- } while (0);
- return isInit;
- }
- /*
- brief 解析字符串,將中英文字符都處理成一個“字節”
- */
- void XCaseStringUtil::parseString()
- {
- int i = 0;
- while (i < _target.length())
- {
- if (!isEnglishChar(_target.at(i)))
- {
- _result.push_back(_target.substr(i, CHINESE_CHAR_LENGTH_UTF8)); // 一個漢字三個字節
- i = i + CHINESE_CHAR_LENGTH_UTF8;
- }
- else
- {
- _result.push_back(_target.substr(i, 1)); // 一個英文一個字節
- i = i + 1;
- }
- }
- }
- /*
- brief 判斷字符是英文還是漢字
- param ch 字符(字節)
- return true:是英文;false:是中文
- */
- bool XCaseStringUtil::isEnglishChar(char ch)
- {
- /*漢字的三個字節(有些編碼格式是兩個字節)的最高爲都爲1,這裏採用判斷最高位的方法
- 將ch字節進行移位運算,右移8位,這樣,如果移位後是0,
- 則說明原來的字節最高位爲0,不是1那麼也就不是漢字的一個字節
- */
- if (~(ch >> 8) == 0)
- {
- return false; //代表不是漢字 此處原文錯誤 應爲是漢字
- }
- return true;
- }
- /*
- brief 獲取字符串長度
- param str 目標字符串
- return 字符串的長度
- */
- int XCaseStringUtil::getLength()
- {
- return _result.size(); // 返回字符串長度
- }
- /*
- brief 截取字符串
- param start 起始下標,從1開始
- param end 結束下標
- param isNeedPoint 是否需要在末尾添加“...”
- return 截取之後的字符串
- */
- string XCaseStringUtil::sub(int start, int end, bool isNeedPoint)
- {
- CCASSERT(getLength() != 0, "string is null");
- CCASSERT(start >= 1 && getLength() >= start, "start is wrong");
- CCASSERT(start <= end, "end is wrong");
- // 容錯處理,如果end大於字符串長度,則捨棄多餘部分
- end = getLength() >= end ? end : getLength();
- string temp = "";
- //直接從_result裏取即可
- for (int i = start; i <= end; i++)
- {
- temp += _result[i - 1];
- }
- // 如果字符串太長,在末尾添加“...”
- if (isNeedPoint)
- {
- if (getLength() > end)
- {
- temp += "...";
- }
- }
- return temp;
- }