How do I remove trailing whitespace from a QString?

前端 未结 9 1771
甜味超标
甜味超标 2020-12-29 19:24

I want to remove all the trailing whitespace characters in a QString. I am looking to do what the Python function str.rstrip() with a QString

9条回答
  •  情话喂你
    2020-12-29 19:48

    No deep copy and no repeated calls to size/chop:

    QString & rtrimInPlace (QString &str) {
        for (int n = str.size() - 1; n >= 0; -- n)
            if (!str.at(n).isSpace()) {
                str.truncate(n + 1);
                break;
            }
        return str;
    }
    

提交回复
热议问题