Home > Mobile >  how to remove space in flutter?
how to remove space in flutter?

Time:09-01

I'm checking two strings whether it is same or not. I used trim(), replaceAll() to clear space in string but still it is showing 21, 22, 23 like that, I want that string to be like 21,22,23 so that I can verify it.

change this 21, 22, 23 into 21,22,23

CodePudding user response:

replaceAll returns a new string and first string does not change.

trim removes leading or trailing whitespace.

This works!

void main() {
  String s = '21, 22, 23';
  String s2 = s.replaceAll(' ', '');
  print(s2);
}

CodePudding user response:

best way is to use regex

String s1 = " your string will be clear \f   from all  spaces   ";
print(s1.replaceAll(new RegExp("[ \n\t\r\f]"), '')); 
//yourstringwillbeclearfromallspaces

CodePudding user response:

Try this:

String text = '21, 22, 23';
text = text.replaceAll(', ', ',');
print(text); //'21,22,23'

CodePudding user response:

try this function

String removeTextSpaces(String value) {
    String stringResult = "";
    value.split(' ').map((e) => stringResult  = e);
    for (var e in value.split(' ')) {
        stringResult  = e;
    }
    return stringResult;
}
  • Related