Qt中去除QString字符串里面多余的空格

问题:QString str(“a  b c            d e”)

=>    QString rstr(“a b c d e”);

解决方法:

void deBlank(QString &strs)
{
    int len = strs.length();
    for (int i=0;i<len;i++)
    {
        if (strs.at(i).isSpace())
        {
            strs[i] = QChar(' ');
        }
    }
}

用法:QString str(“a b        c”);

str = deBlank(str);

经过转换后str的内容就为"a b c”。

Qt里自带的方法:

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.

Whitespace means any character for which QChar::isSpace() returns true. This includes the ASCII characters '\t', '\n', '\v', '\f', '\r', and ' '.

Example:

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

你可能感兴趣的:(c,String,qt,character,each,whitespace)