52

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

braulio.cassule
  • 1,322
  • 4
  • 14
  • 23

9 Answers9

83

This would solve your problem

String name = "COCA COLA";
print(name.replaceAll(' ', ''));
Christopher Moore
  • 15,626
  • 10
  • 42
  • 52
Kofi Nartey
  • 2,529
  • 2
  • 9
  • 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
64

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
  • 1,076
  • 8
  • 18
11

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
  • 343
  • 1
  • 2
  • 9
9

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
  • 4,690
  • 23
  • 42
5

You can try this:

String product = "COCA COLA";

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

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

Sbr777
  • 51
  • 2
3

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(' ', '');
  }
}
2

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
  • 423
  • 6
  • 12
-3

Use Trim Function

String name = "Stack Overflow";
print(name.trim());
SilenceCodder
  • 2,874
  • 24
  • 28