42

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

Paresh Mangukiya
  • 37,512
  • 17
  • 201
  • 182
braulio.cassule
  • 1,072
  • 4
  • 12
  • 21

9 Answers9

69

This would solve your problem

String name = "COCA COLA";
print(name.replaceAll(' ', ''));
Christopher Moore
  • 12,922
  • 9
  • 35
  • 46
Kofi Nartey
  • 2,049
  • 2
  • 7
  • 5
  • How remove different occurence in same string? Like my string is "hello%25sachin%20here " , I wanti ti remove '%25' and '%20" both ...... – Sachin Jun 15 '20 at 07:25
49

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(' ', '');
Fobos
  • 916
  • 6
  • 18
9

the Trim method just remove the leading and trailing. Use Regexp instide: Here is an example: Dart: Use regexp to remove whitespaces from string

ShrJamal
  • 313
  • 1
  • 2
  • 7
3

I know that this question has pretty good answers, but I want to show a fancy way to remove all whitespace in a string. I actually thought that Dart should've had a built-in method to handle this, so I created the following extension for String class:

extension ExtendedString on String {
  /// The string without any whitespace.
  String removeAllWhitespace() {
    // Remove all white space.
    return this.replaceAll(RegExp(r"\s+"), "");
  }
}

Now, you can use it in a very simple and neat way:

String product = "COCA COLA";
print('Product id is: ${product.removeAllWhitespace()}');
Stewie Griffin
  • 3,013
  • 15
  • 33
2

You can try this:

String product = "COCA COLA";

print(product.split(" ").join(""));   // COCACOLA
mdev
  • 21
  • 4
1

In case this is of any help to someone in the future, for convenience you can define an extension method to the String class:

extension StringExtensions on String {
  String removeWhitespace() {
    return this.replaceAll(' ', '');
  }
}

This can be called like product.removeWhiteSpace() I've used it in the past to create a helper method when sorting lists by a string whilst ignoring case and whitespace

extension StringExtensions on String {
  String toSortable() {
    return this.toLowerCase().replaceAll(' ', '');
  }
}
0

There are so many ways to do this.

The most obvious one:

String product = "COCA COLA";
print('Product id is: ${product.replaceAll(' ', '')}');  // COCACOLA

With regular function:

String removeAllWhitespaces(String string) {
  return string.replaceAll(' ', '');
}

String product = "COCA COLA";
print('Product id is: ${removeAllWhitespaces(product)}');  // COCACOLA

With extension method:

extension StringRemoveWhitespace on String { 
String get removeAllWhitespaces => replaceAll(' ', '');  // COCACOLA
}

String product = "COCA COLA";
print('Product id is: ${product.removeAllWhitespaces}');
Talat El Beick
  • 265
  • 5
  • 8
0

var s = "Coca Cola"; s.replaceAll(' ','');

Sbr777
  • 11
  • 1
-3

Use Trim Function

String name = "Stack Overflow";
print(name.trim());
SilenceCodder
  • 3,014
  • 20
  • 30