QT 从 QString 中获取被特殊符号分隔的子串

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

方法一:利用split

     QString str = "test1, st2 ,jf,";
     QList<QString> lst;
     lst.clear();
     lst=str.split(',');
     qDebug()<<"lst:"<<lst;
     qDebug()<<"lst.count():"<<lst.count();
     for(int i;i<lst.count();i++)
     {
         qDebug()<<"lst["<<i<<"]"<<lst[i];
     }   

运行结果:
在这里插入图片描述

方法二:利用section

     QString str = "test1, st2 ,jf,";
     qDebug()<<"str.section(',',1,1):"<<str.section(',',1,1);//获得第一段
     qDebug()<<"str.section(',',0,1):"<<str.section(',', 0,1);//从第0段起,到第1段止

运行结果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_33308135/article/details/82991758