Home > other >  How to avoid multiple spaces in textfields?
How to avoid multiple spaces in textfields?

Time:12-06

Is there a way to stop the use of more than 1 space in a row in a textfield?.

Currently I am using this code to deal with a single space, but I don't want to end up with an infinitely long list of exceptions like the second image.

I do want to use spaces, just not at the start of my text or multiple spaces in a row.

Thank you in advance.

CodePudding user response:

You could use trimRight() to strip all leading whitespace from your value.

If you want to deal with trailing whitespace as well, you could just use trim()

Either way, both of these would catch all the exceptions you listed, whether there is an empty value, or a value with only whitespace.

I'm unsure if you are asking how to also replace multiple spaces inside the string with just single space? To do that you can use a regex, like this:

    value = value.trim();
    var singleSpaces = value.replaceAll(RegExp(r"\s "), " ");
  • Related