How to remove all whitespace of a string in Dart?
This would solve your problem
String name = "COCA COLA";
print(name.replaceAll(' ', ''));
the Trim method just remove the leading and trailing. Use Regexp instide: Here is an example: Dart: Use regexp to remove whitespaces from string
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()}');
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(' ', '');