Qt 判断字符串满足某种格式

没有用正则表达式,比较原始的方法

比如说文件格式为“IMG_20161214_000001.jpg”,中间为日期格式,最后为数字编号,要判断一个文件的名称是否符合上述规则。


输入:文件的名称

输入:符合规则返回true,不符合返回false

说明:将输入的名称字符串按照下划线“_”分割,分成3段,依次判断这3段是否符合要求

bool UDiskUserManager::checkFileName(char *fileName)
{
    QString strFileName(fileName);
    QStringList list =  strFileName.split("_");
    int digit =0;
    if(list[0] == "IMG")   //第一段
    {
        QString strDate = list[1];
        if(strDate.size() == 8)
        {
            int year = strDate.left(4).toInt();
            int month = strDate.mid(4,2).toInt();
            int day = strDate.left(2).toInt();
            if( (year>1900) && (year <2099) && (month >0) && (month <13) && (day < 32))   //第二段
            {
                QString strCount = list[2].left(6);
                if(strCount.size() == 6)
                {
                    char cCount[6] = {strCount.at(0).toLatin1(), strCount.at(1).toLatin1(), strCount.at(2).toLatin1(),strCount.at(3).toLatin1(),strCount.at(4).toLatin1(),strCount.at(5).toLatin1() };
                    for(int i=0; i<6; i++)
                    {
                        if( (0<=cCount[i]) || (cCount[i]<= 9)) //第三段,依次判断每一位是否为数字
                        {
                            digit++;
                            continue;
                        }
                        else
                        {
                            return false;
                        }
                    }
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }
    else
    {
        return false;
    }
    if(digit == 6)
    {
        return true;
    }
}

方法比较笨,如果有更好的办法,还请指正。


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值