Dart: Capitalize the First Letter of Each Word in a String

Last updated on July 18, 2021 A Goodman Loading... Post a comment

The example below shows you how to capitalize the first letter of each word in a string in Dart (and Flutter as well).

The code:

void main() {
  String totTitle(String input) {
    final List<String> splitStr = input.split(' ');
    for (int i = 0; i < splitStr.length; i++) {
      splitStr[i] =
          '${splitStr[i][0].toUpperCase()}${splitStr[i].substring(1)}';
    }
    final output = splitStr.join(' ');
    return output;
  }

  final str1 = 'Kindacode.com is a website about programming.';
  final str2 = 'Today is a raining day!';
  print(totTitle(str1));
  print(totTitle(str2));
}

Output:

Kindacode.com Is A Website About Programming.
Today Is A Raining Day!

Further reading:

You can also check out our Flutter category page, or Dart category page for the latest tutorials and examples.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

You May Also Like