去除字符串前面的空格
QString str1 = " 1 2 3 "; str1.remove(QRegExp("^ +\\s*")); qDebug() << "str1=" << str1;
去除字符串后面的空格
QString str2 = " 4 5 6 "; str2.remove(QRegExp("\\s* +$")); qDebug() << "str2=" << str2;
去除字符串所有的空格
QString str3 = " 6 7 8 "; str3.remove(QRegExp("\\s")); qDebug() << "str3=" << str3;
去除字符串首尾的空格
//\t,\n,\v,\f,\r都属于simplified()处理的空白范围 QString str4 = " \t,\n,\v,\f,\rhello\t world\n123\r\n "; qDebug() << "str4.simplified()=" << str4.simplified(); QString str5 = " \t,\n,\v,\f,\rhello\t world\n123\r\n "; qDebug() << "str5.trimmed()=" << str5.trimmed();