Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove trailing whitespace from a QString?

Tags:

c++

string

trim

qt

qt4

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.

I did some Googling, and found this: http://www.qtforum.org/article/20798/how-to-strip-trailing-whitespace-from-qstring.html

So what I have right now is something like this:

while(str.endsWith( ' ' )) str.chop(1); while(str.endsWith( '\n' )) str.chop(1); 

Is there a simpler way to do this? I want to keep all the whitespace at the beginning.

like image 356
Di Zou Avatar asked Nov 21 '11 16:11

Di Zou


People also ask

How do I remove spaces from QString?

[ QString::simplified ] 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. Option 2: Use a QRegExp to capture all types of white space in remove .

How do you get rid of trailing white space?

Type M-x delete-trailing-whitespace to delete all trailing whitespace. This command deletes all extra spaces at the end of each line in the buffer, and all empty lines at the end of the buffer; to ignore the latter, change the variable delete-trailing-lines to nil .

How do you get rid of leading and trailing white spaces?

To remove leading and trailing spaces in Java, use the trim() method. This method returns a copy of this string with leading and trailing white space removed, or this string if it has no leading or trailing white space.


2 Answers

QString has two methods related to trimming whitespace:

  • QString QString::trimmed() const
    Returns a string that has whitespace removed from the start and the end.
  • 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.

If you want to remove only trailing whitespace, you need to implement that yourself. Here is such an implementation which mimics the implementation of trimmed:

QString rstrip(const QString& str) {   int n = str.size() - 1;   for (; n >= 0; --n) {     if (!str.at(n).isSpace()) {       return str.left(n + 1);     }   }   return ""; } 
like image 81
Frank S. Thomas Avatar answered Sep 28 '22 05:09

Frank S. Thomas


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" 
like image 37
Andrejs Cainikovs Avatar answered Sep 28 '22 06:09

Andrejs Cainikovs