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
To trim leading and trailing spaces or white space characters of a given string in Dart, you can use trim() method of String class. String. trim() returns a new string with all the leading and trailing white spaces of this string removed.
trim() removes the spaces from beginning and end of string.
strip()—Remove Leading and Trailing Spaces. The str. strip() method removes the leading and trailing whitespace from a string.
We have used the replaceAll() method on string with RegEx expression to remove the special characters. It will give you the alphabets and numbers both and Special characters will be removed.
This would solve your problem
String name = "COCA COLA"; print(name.replaceAll(' ', ''));
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(' ', '');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With