Debug School

rakesh kumar
rakesh kumar

Posted on

How to remove leading and trailing whitespaces in Flutter

** Solution:**

User can try the below solutions:

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

Update:

String name = '4 ever 1 k g @@ @';
print(name.replaceAll(new RegExp(r"\s+"), ""));
Enter fullscreen mode Exit fullscreen mode

This would solve your problem

String name = "COCA COLA";
print(name.replaceAll(' ', ''));
Enter fullscreen mode Exit fullscreen mode

Use Trim Function():

String name = "Stack Overflow";
print(name.trim());
Enter fullscreen mode Exit fullscreen mode

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(' ', '');
  }
}
Enter fullscreen mode Exit fullscreen mode

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(' ', '');
  }
}
Enter fullscreen mode Exit fullscreen mode

Using trim(): trim() method is used to remove leading and trailing whitespaces. It doesn’t mutate the original string. If there is no whitespace at the beginning or the end of the String, it will return the original value.

print('   COCA COLA'.trim()); // Output: 'COCA COLA'
print('COCA COLA     '.trim()); // Output: 'COCA COLA'
print('   COCA COLA     '.trim()); // Output: 'COCA COLA'
Enter fullscreen mode Exit fullscreen mode

Using trimLeft() and trimRight(): What if you want to trim at the beginning only but not at the end, or maybe the opposite way. You can use trimLeft for removing leading whitespaces only and trimRight which removes trailing whitespaces only.

print('   COCA COLA    '.trimLeft()); // Output: 'COCA COLA     '
print('   COCA COLA    '.trimRight()); // Output:'   COCA COLA'
Enter fullscreen mode Exit fullscreen mode

If the String can be null, you can consider using the null-aware operator.

String s = null;
print(s?.trim());
Enter fullscreen mode Exit fullscreen mode

The above code will return null instead of throwing NoSuchMethodError.

Using regular expression (RegExp): If the original string contains multiple whitespaces and you want to remove all whitespaces.
String replaceWhitespacesUsingRegex(String s, String replace) {

 if (s == null) {
   return null;
 }

 // This pattern means "at least one space, or more"
 // \\s : space
 // +   : one or more
 final pattern = RegExp('\\s+');
 return s.replaceAll(pattern, replace);
}
Enter fullscreen mode Exit fullscreen mode

Call like a below:

print(replaceWhitespacesUsingRegex('One  Two   Three   Four', ''));
Enter fullscreen mode Exit fullscreen mode

//

 Output: 'OneTwoThreeFour'

Enter fullscreen mode Exit fullscreen mode

print(' COCA COLA'.trim()); // Output: 'COCA COLA'
print('COCA COLA '.trim()); // Output: 'COCA COLA'
print(' COCA COLA '.trim()); // Output: 'COCA COLA'

Image description
Refrence1

Top comments (0)