一、按字符分割
多字符
[ ]内填入的字符将会作为切割字符串的标记,可输入\t\,tab键,空格键等。
QString str = "/home|cyfeng/mycpp/ FinalAUI3/mainwindows.cpp";
QStringList list = str.split(QRegExp("[/. \t|]"));
QString splitstr;
for(int i=0;i<list.size();i++){
splitstr = list[i];
cout<<i<<" "<<splitstr.toStdString()<<endl;
}
输出:
0
1 home
2 cyfeng
3 mycpp
4
5 FinalAUI3
6 mainwindows
7 cpp
单字符
QString str = "/home/cyfeng/mycpp/FinalAUI3/mainwindows.cpp";
QStringList list = str.split("/");
QString splitstr;
for(int i=0;i<list.size();i++){
splitstr = list[i];
cout<<i<<" "<<splitstr.toStdString()<<endl;
}
输出:
0
1 home
2 cyfeng
3 mycpp
4 FinalAUI3
5 mainwindows.cpp
获取
QString str = "/home/cyfeng/mycpp/FinalAUI3/mainwindows.cpp";
QStringList list = str.split("/");
QString splitstr;
splitstr = list.first();
cout<<"first\t"<<splitstr.toStdString()<<endl;
splitstr = list.last();
cout<<"last\t"<<splitstr.toStdString()<<endl;
splitstr = list.at(3);
cout<<"at(3)\t"<<splitstr.toStdString()<<endl;
splitstr = list[4];
cout<<"[4]\t"<<splitstr.toStdString()<<endl;
输出:
first
last mainwindows.cpp
at(3) mycpp
[4] FinalAUI3
二、按位置分割
QString str = "1234567890abcdefghijkl";
QString splitstr;
splitstr = str.mid(4,3);
cout<<"mid\t"<<splitstr.toStdString()<<endl;
splitstr = str.left(5);
cout<<"left\t"<<splitstr.toStdString()<<endl;
splitstr = str.right(6);
cout<<"right\t"<<splitstr.toStdString()<<endl;
输出:
mid 567
left 12345
right ghijkl