Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing whitespaces inside a string

Tags:

c++

qt

qstring

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.

like image 925
Gandalf Avatar asked Nov 24 '11 11:11

Gandalf


People also ask

How do you remove all whitespaces in a string in python?

Python String strip() function will remove leading and trailing whitespaces. If you want to remove only leading or trailing spaces, use lstrip() or rstrip() function instead.

How do you remove whitespaces from a given string in JavaScript?

JavaScript String trim() The trim() method removes whitespace from both sides of a string. The trim() method does not change the original string.

How do you remove the middle space in a string in Java?

Java has inbuilt methods to remove the whitespaces from the string whether leading, trailing, both, or all. trim() method removes the leading and trailing spaces present in the string. strip method removes the leading and trailing spaces present in the string. Also, it is Uniset/Unicode character aware.

How will you remove the whitespaces from both sides of the string?

trim() The trim() method removes whitespace from both ends of a string and returns a new string, without modifying the original string. Whitespace in this context is all the whitespace characters (space, tab, no-break space, etc.) and all the line terminator characters (LF, CR, etc.).


2 Answers

str = str.simplified(); str.replace( " ", "" ); 

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

like image 110
arnt Avatar answered Sep 22 '22 20:09

arnt


Try this:

str.replace(" ",""); 
like image 44
tonekk Avatar answered Sep 20 '22 20:09

tonekk