How do I remove trailing whitespace from a QString?

前端 未结 9 1774
甜味超标
甜味超标 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:40

    QString provides only two trimming-related functions. In case if they don't suit your needs, I'm afraid you need to implement your own custom trimming function.

    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.

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

    QString QString::trimmed () const
    Returns a string that has whitespace removed from the start and the end.

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

提交回复
热议问题