Removing whitespaces inside a string

Gandalf picture Gandalf · Nov 24, 2011 · Viewed 37.1k times · Source

I have a string lots\t of\nwhitespace\r\n which I have simplified but I still need to get rid of the other spaces in the string.

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

I can do this erase_all(str, " "); in boost but I want to remain in qt.

Answer

arnt picture arnt · Nov 24, 2011
str = str.simplified();
str.replace( " ", "" );

The first changes all of your whitespace characters to a single instance of ASCII 32, the second removes that.