Reputation: 49
How can I convert text with too many spaces to single space? For example name: "Brad Pit" there is only one space between first and last name but user can write "Brad (4 space) Pit" with too much space. How can I reduce this to single space in TextFormField?
Upvotes: 1
Views: 877
Reputation: 62401
Code:
void main() {
var name = 'Pratik Butani';
print(name);
print(name.replaceAll(new RegExp(r"\s+"), " "));
}
Output:
Pratik Butani
Pratik Butani
Explanation: Here I have used RegExp which will take continue spaces using \s+ and It will be replaced by one space.
You can this demo on DartPad
Do let me know if you have any confusion.
Upvotes: 2
Reputation: 85
In Dart u can use:
print(yourString.trim())
to convert text with too many spaces
and u can try this
String name = 'Brad Pit ';
print(name.replaceAll(new RegExp(r"\s+"), ""));
Upvotes: 1
Reputation: 81
To reduce multiple space to a single space Please refer to this answer https://stackoverflow.com/a/1981366/9414608
Also you can prevent users from adding spaces in the TextFormField by using inputFormatters
Please refer to this answer https://stackoverflow.com/a/59065304/9414608
flutterdartwhitespacestextformfieldstring
Upvotes: 0