Home > OS >  How to remove whitespace within in a string in Flutter / dart and capitalize first letter of each wo
How to remove whitespace within in a string in Flutter / dart and capitalize first letter of each wo

Time:07-30

I want to remove white space within a string like extra spaces in between the words. I have tried trim() method. but it only removes the leading and trailing whitespace I want to remove the spaces in between the string and I want to convert the first letter of the each word to capital . example : var name = ' Aneesh devala ' to Aneesh Devala

I have tried this answers but it's not suitable for mine.

CodePudding user response:

I hope this code will work for you.

String getCapitalizedName(String name) {
final names = name.split(' ');
String finalName = '';
for (var n in names) {
  n.trim();
  if (n.isNotEmpty) {
    finalName  = '${n[0].toUpperCase()}${n.substring(1)} ';
  }
}
return finalName.trim();}
  • Related