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

Updated: March 31, 2023 By: A Goodman One comment

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

The code:

// KindaCode.com
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;
  }

  const str1 = 'Kindacode.com is a website about programming.';
  const 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!

That’s it. 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
1 Comment
Inline Feedbacks
View all comments
Davoud
Davoud
1 year ago

Awsome!

Related Articles