QString与int的转换
- 转数字
QString str = "999"; int num = str.toInt(&isOk); //若isOk为true则转换成功 // num == 999
- 转QString
QString str_; str_ = QString::number(999); // str_ == "999"
replace() 替换字符
QString &replace(int i, int len, const QString &after);
//参数:位置索引为i、将替换字符个数、将替换字符串引用,返回类型为字符串引用
QString str("hello world!");
str.replace(6, 5, "wcx"); //从索引6开始将5个字符替换为"wcx"
// str == "hello wcx!"
remove() 移除字符
QString &remove(int i, int len);
//参数:位置索引、移除长度,返回类型为字符串引用
//函数从字符串的i处开始移除len个字符并返回此字符串的引用(若i超出字符串长度,则无意义)
QString str("hello world");
str.remove(5,6);
//str == "hello";
QString str("h e l l o _world");
str.remove(QRegExp("\\s"); //使用正则除去空格
//str == "hello_world";
leftJustified() 左对齐填充字符
QString leftJustified(int width, QChar fill = QLatin1Char(' '), bool trunc = false) const;
//参数:width:填充后字符总个数、 填补的字符、 trunc默认为假,若trunc为假且字符串长度超过width,返回的字符串是这个字符串的复制,若为真且字符串长度超过width,那么这个字符串的复制中超过width长度的任何字符都会被移除,然后返回字符串的复制
//函数将返回一个长度为width(包含填补的字符)的字符串。
QString str("hello world");
QString str_ = str.leftJusttified(16, '!');
// str_ == "hello world!!!!!"