目的
QString判断是否包含中文。
实现方式
可以通过以下两种方式实现:
- 逐一取QString的字符,判断其Unicode码位是否处于中文的范围之内。
- 具体代码实现如下:
QString str;
int nCount = str.count();
bool containsChinese = false;
for(int i = 0; i < nCount; i++) { QChar cha = str.at(i); ushort uni = cha.unicode(); if(uni >= 0x4E00 && uni <= 0x9FA5) { //这个字符是中文 containsChinese = true; break; }
}
- 使用正则表达式进行判断。具体代码实现如下:
QString str;
bool b = str.contains(QRegExp("[\\x4e00-\\x9fa5]+")); //使用正则表达式判断是否包含中文字符
if(b) { //存在中文
}
以上两种方法都可以实现QString判断是否包含中文的功能。