C++ 计算字符创长度之Function(包含unicode,utf-8),包含特殊字符

2014-11-24 09:25:39 · 作者: · 浏览: 0



//字符串长度

int calcCharCount(const char *pszText)

{

int n = 0;

char ch = 0;

while ((ch = *pszText))

{

CC_BREAK_IF(! ch);

if ((0x80 & ch) == 0x00) {

n++;

} else if (0x80 != (0xC0 & ch)) {

n+=2;

}

++pszText;

}

return n;

}




//包含特殊字符

bool checkSpecialCharacter(string text)

{

char temp;

for (int i =0; i

temp = text[i];

if ((temp >= 0 && temp <= 47) || (temp >= 58 && temp<= 64) || (temp >=91 && temp <= 96) || (temp >= 123 && temp <= 223 )) {

CCLog("用户名称包含特殊字符");

return true;

}

}

return false;

}