Home > Enterprise >  Flutter remove extra white space between string
Flutter remove extra white space between string

Time:09-17

how to remove extra white space between following string ? String word= "Hai where are you from" ; word.split(" ") does not work for this condition.

CodePudding user response:

this will remove the white space in the string

try

String word= "Hai where are you from";
print(word.replaceAll(' ', ''));

CodePudding user response:

You can you two functions for String: ReplaceAll Function

String str;
  str.replaceAll(" ", " ");

Trim function

str.trim();

CodePudding user response:

If you want to remove extra white spaces you can use this code. Just call the cleanupWhitespace method and it will return the cleaned up string.

final whitespaceRE = RegExp(r"(?! )\s | \s ");
String cleanupWhitespace(String input) => input.split(whitespaceRE).join(" ");

It works like this for ex if you have a string like this:

Hello                   world

It will replace all the white space with a single white space.

Hello world
  • Related