Qt开发之路57---判断字符串是否为纯英文、纯数字、纯中文

这段代码包含三个函数,用于检测输入的QString是否只包含英文字符、数字或中文字符。boolIsEnglish()检查字符串是否全为英文,boolIsNumber()验证是否全为数字,boolIsChinese()则确认是否全部由中文组成。这些函数通过遍历字符串并比较每个字符的Unicode值来实现其功能。
摘要由CSDN通过智能技术生成

一:判断是否为纯英文

bool IsEnglish(QString str)
{
    QByteArray ba = str.toLatin1();
    const char *ch = ba.data();
    while (*ch) {
        if((*ch >= 'A' && *ch <= 'Z')||(*ch >= 'a' && *ch <= 'z')){

        }else {
            return false;
        }

        ch++;
    }

    return true;

}

二:判断是否为纯数字

bool IsNumber(QString str)
{
    QByteArray ba = str.toLatin1();
    const char *ch = ba.data();
    while (*ch) {
        if(*ch >= '0' && *ch <= '9'){

        }else {
            return false;
        }

        ch++;
    }

    return true;
}

三:判断是否为纯中文

bool IsChinese(QString str)
{
    int count = str.count();

    for(int i = 0; i < count; i++){
        QChar ch = str.at(i);
        ushort us = ch.unicode();
        if(us >= 0x4E00 && us <= 0x9FA5){

        }else {
            return false;
        }

    }

    return true;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值