QT如何去掉字符串中的空格

2014-11-24 03:28:25 · 作者: · 浏览: 0

这里给大家介绍QString中的两个函数

1.QString QString::simplified() const

Returns a string that has whitespace removed from the start and the end, and that has each sequence of internal whitespace replaced with a single space.

返回一个字符串,移除从一开始到结尾的空白,每个序列内部的空格替换为一个空格(头尾的都去掉了)


举个例子:[cpp]
QString str = " lots\t of\nwhitespace\r\n ";
str = str.simplified();
// str == "lots of whitespace";

QString str = " lots\t of\nwhitespace\r\n ";
str = str.simplified();
// str == "lots of whitespace";'\t', '\n', '\v', '\f', '\r', ' ' 都属于空白的处理范围。

2.QString QString::trimmed() const

Returns a string that has whitespace removed from the start and the end.

返回一个字符串,移除从一开始到结尾的空白。也去掉头尾的空白

举个例子:


[cpp]
QString str = " lots\t of\nwhitespace\r\n ";
str = str.trimmed();
// str == "lots\t of\nwhitespace"

QString str = " lots\t of\nwhitespace\r\n ";
str = str.trimmed();
// str == "lots\t of\nwhitespace"