Qt中获取字符串中的汉字,判断是否有汉字,判断特定字符

 
  
 
//提取字符串中的中文字符串
QString myFile::getChinese(QString &str)
{
    //    QString str;
    QString chineseStr;
    int nCount = str.count();
    for(int i = 0 ; i < nCount ; i++)
    {
        QChar cha = str.at(i);
        ushort uni = cha.unicode();
        if(uni >= 0x4E00 && uni <= 0x9FA5)
        {
            //这个字符是中文
            qDebug()<<uni;
            chineseStr.append(uni);
            //            qDebug("%s",uni);
        }
    }
    return chineseStr;  //最后返回的这个字符串就是中文字符串

}


//判断字符串中是否含有中文字符,可以使用强大的正则表达式
QString str;
str.contains(QRegExp("[\\x4e00-\\x9fa5]+")

//当然如果我们只想判断字符串中是否有特定的中文字符,就更简单了
str.contains("中国");


猜你喜欢

转载自blog.csdn.net/zxl_1996/article/details/79303850