Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove all whitespace of a string in Dart?

Using trim() to eliminate white space in Dart and it doesn't work. What am I doing wrong or is there an alternative?

       String product = "COCA COLA";         print('Product id is: ${product.trim()}'); 

Console prints: Product id is: COCA COLA

like image 351
braulio.cassule Avatar asked Jul 24 '18 22:07

braulio.cassule


People also ask

How do you get rid of white spaces in a string Dart?

To trim leading and trailing spaces or white space characters of a given string in Dart, you can use trim() method of String class. String. trim() returns a new string with all the leading and trailing white spaces of this string removed.

How do you remove all spaces from a string flutter?

trim() removes the spaces from beginning and end of string.

How do I remove spaces between strings?

strip()—Remove Leading and Trailing Spaces. The str. strip() method removes the leading and trailing whitespace from a string.

How do you remove strings in flutter?

We have used the replaceAll() method on string with RegEx expression to remove the special characters. It will give you the alphabets and numbers both and Special characters will be removed.


2 Answers

This would solve your problem

String name = "COCA COLA"; print(name.replaceAll(' ', '')); 
like image 55
Kofi Nartey Avatar answered Sep 25 '22 04:09

Kofi Nartey


Try this

String product = "COCA COLA"; print('Product id is: ${product.replaceAll(new RegExp(r"\s+\b|\b\s"), "")}'); 

Update:

String name = '4 ever 1 k g @@ @'; print(name.replaceAll(RegExp(r"\s+"), "")); 

Another easy solution:

String name = '4 ever 1 k g @@ @'; print(name.replaceAll(' ', ''); 
like image 30
Fobos Avatar answered Sep 26 '22 04:09

Fobos