QT中QString类的各种使用
大部分的QString使用可以参考:QT中QString 类的使用--获取指定字符位置、截取子字符串等_qstring 取子串-CSDN博客
补充一种QString类的分离:Qt QString切割(Split()与Mid()函数详解)_qstring split-CSDN博客
1. Trimmed和Simplified函数(去除空白)
- trimmed:去除了字符串开头前和结尾后的空白;
- simplified:去除了字符串开头前和结尾后的空白,以及中间内部的空白字符也去掉(\t','\n','\v','\f','\r'和' ')
代码:
QString str = " 1 2 3 4 5 \t \n ABCDE ";
str = str.trimmed();
qDebug() << "str : " << str ;
str = str.simplified();
qDebug() << "str : " << str ;
结果:
2. Left,Right,Mid函数
参考:QT QString中mid()、left()、right()函数_qstring mid-CSDN博客
3. double,float,int(QString::number)相互转换QString(toDouble,ToFloat,ToInt)
代码:
double f = 3.1415259;
float a = 1.3556;
QString str = "123";
QString str_1 = "123.1232";
f = QString::number(f,'f',2).toDouble();
a = QString::number(a,'f',3).toFloat();
int b = str.toInt();
float c = str_1.toFloat();
double e = str_1.toDouble();
int str_a = 20;
float str_f = 30.556;
str = QString::number(str_a);
str_1 = QString::number(str_f,'f',2);
qDebug() << "f == "<< f << " a == " <<a ;
qDebug() << "b == "<< b << " c == " <<c << " e == " << e ;
qDebug() << " str == " << str << " str_1 == "<< str_1;
结果:
想要了解QString::number函数可以参考:QString类—number_qstring::number-CSDN博客