1. 程式人生 > >Cocos2dx中英文混合字串擷取

Cocos2dx中英文混合字串擷取

一、定義標頭檔案

#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;
}