std::string textStr = element.text;
std::string lines;
int len = 40;
if(element.text.length() > len){
for(int cur = 0; cur < len;){
char t = textStr[cur];
if((t&0xE0) == 0xE0){ //3byte
lines = lines + t + textStr[cur+1] + textStr[cur+2];
cur += 3;
}else if((t & 0xC0) == 0xC0){//2byte
lines = lines + t + textStr[cur+1];
cur += 2;
}else { //1byte
lines = lines + t;
cur += 1;
}
}
textStr = lines + ”
“;
CCLog(“streaming text:%s”, lines.c_str());
///////////////////////////
方法二
#pragma mark 字符串换行重组
std::string lineFeeds(const std::string &str, int iCurrentIndex)
{
if (typeid(str) == typeid(std::string)
&& str.length() > 0 &&iCurrentIndex > 0) {
std::stringstream result;
std::vector<<span class="s2">std::string> temp;
temp = parseUTF8(str);
int iStringLength = temp.size();
for (int i = 0;i < iStringLength;i++) {
result<<temp[i];
if (iCurrentIndex == i)
result<<"\n";
}
return result.str();
} else {
return "";
}
}
#pragma mark UTF8字符解析
std::vector<<span class="s2">std::string> parseUTF8(const std::string &str)
{
int l = str.length();
std::vector<</span>std::string>
ret;
ret.clear();
for(int p = 0; p < l; ) {
int size;
unsigned char c = str[p];
if(c < 0x80) {
size = 1;
} else if(c < 0xc2) {
} else if(c < 0xe0) {
size = 2;
} else if(c < 0xf0) {
size = 3;
} else if(c < 0xf8) {
size = 4;
} else if (c < 0xfc) {
size = 5;
} else if (c < 0xfe) {
size = 6;
}
std::string temp = "";
temp = str.substr(p, size);
ret.push_back(temp);
p += size;
}
return ret;
}
#pragma mark 截取UTF8字符串
std::string subUTF8(const std::string &str,int from, int to)
{
if(from > to) return "";
vector<<span class="s2">std::string> test = parseUTF8(str);
if (test.size() < to) return str;
vector<<span class="s2">string>::iterator iter = test.begin();
std::string res;
std::string result;
for(iter=(test.begin() + from); iter != (test.begin() + to); iter++)
{
res += *iter;
}
return res;
}