Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to Capitalized each first letter for a Sentence in Flutter…?

I want to capitalize each first letter from a sentence for Flutter..??

This is a capitalized sentence
I’m expecting from This Is A Capitalized Sentence

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

extension StringExtension on String {
  String capitalizeByWord() {
    if (trim().isEmpty) {
      return '';
    }
    return split(' ')
        .map((element) =>
            "${element[0].toUpperCase()}${element.substring(1).toLowerCase()}")
        .join(" ");
  }
}

Use extension like this. So that you can use capitalizeByWord on any String to convert it.

void main() async {
  var data = 'this is a capitalized sentence';
  print(data.capitalizeByWord()); // This prints the result 'This Is A Capitalized Sentence'
}

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading