Reputation: 1449
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
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
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
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
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
Reputation: 2817
This would solve your problem
String name = "COCA COLA";
print(name.replaceAll(' ', ''));
Upvotes: 96
Reputation: 51
You can try this:
String product = "COCA COLA";
print(product.split(" ").join("")); // COCACOLA
Upvotes: 5
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
Reputation: 3174
Use Trim Function
String name = "Stack Overflow";
print(name.trim());
Upvotes: -3
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