字符串非数字替换 C QString方式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33557833/article/details/77530723

    //c方式
    char str[10];
    int i = 0;
    strcpy(str,"7!000");
    int len = strlen(str);
    qDebug()<<"str = "<<str;
    for(i = 0;i<len;i++)
    {
        if( ( str[i]>=0x30 ) && ( str[i]<=0x39 ))
        {
             continue;
        }
        else
        {
            str[i]=0x2E;
        }
    }
    
    qDebug()<<"str = "<<str;
    
    //QString 当成 char类型的数组一个一个判断
    QString tmp = "10r77";
    qDebug()<<"tmp = "<<tmp;
    for(i = 0;i<tmp.size();i++)
    {
    if( ( tmp[i]>=0x30 ) && ( tmp[i]<=0x39 ))
    {
    continue;
    }
    else
    {
    tmp[i]=0x2E;
    }
    }
    
    qDebug()<<"tmp = "<<tmp;
    
    //QString.replace+QRegExp 判断  
    QString tmp1 = "10r77";
    qDebug()<<"tmp1 = "<<tmp1;
    tmp1 = tmp1.replace(QRegExp("[^\\d+]"),".");
    qDebug()<<"tmp1 = "<<tmp1;


运行结果如下:

str = 7!000

str = 7.000

tmp = "10r77"

tmp = "10.77"

tmp1 = "10r77"

tmp1 = "10.77"




猜你喜欢

转载自blog.csdn.net/qq_33557833/article/details/77530723