braulio.cassule
braulio.cassule

Reputation: 1449

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

Upvotes: 57

Views: 98277

Answers (10)

Prakash pratap Singh
Prakash pratap Singh

Reputation: 113

String name = "COCA COLA";
print(name.replaceAll(' ', ''));

The above solution is correct but it doesn't remove the spaces permanently. So, to remove spaces permanently from the string we can use the below code,

String name = "COCA COLA";
name = name.replaceAll(' ', '');
print(name);

Upvotes: 0

Fobos
Fobos

Reputation: 1156

Try this

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

Update:

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

Another easy solution:

String name = '4 ever 1 k g @@ @';
debugPrint(name.replaceAll(' ', ''));

Upvotes: 72

Sbr777
Sbr777

Reputation: 71

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

Upvotes: 6

Talat El Beick
Talat El Beick

Reputation: 531

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

Upvotes: 3

Stewie Griffin
Stewie Griffin

Reputation: 5608

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

Upvotes: 10

Kofi Nartey
Kofi Nartey

Reputation: 2817

This would solve your problem

String name = "COCA COLA";
print(name.replaceAll(' ', ''));

Upvotes: 96

mdev
mdev

Reputation: 51

You can try this:

String product = "COCA COLA";

print(product.split(" ").join(""));   // COCACOLA

Upvotes: 5

Giles Correia Morton
Giles Correia Morton

Reputation: 872

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

Upvotes: 3

SilenceCodder
SilenceCodder

Reputation: 3174

Use Trim Function

String name = "Stack Overflow";
print(name.trim());

Upvotes: -3

ShrJamal
ShrJamal

Reputation: 353

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

Upvotes: 11

Related Questions