Dart capitalize first letter in a String example

In these examples, Learn how to capitalize the first letter of a string or statement in Dart programming examples.

The string contains a group of words separated by spaces and enclosed in single or double quotes.

For example, if the string contains ‘hello world’, capitalize of a string is ‘Hello world’.

How to capitalize the first letter of a string in Dart

In this example, the Written capitalizeFirstLetter function

  • Takes input as String and returns String type
  • In the function, Convert the first letter to Uppercase, appended to a substring excluding the first character.
  • This function is called from the main function

Here is an example

void main() {
    print(capitalizeFirstLetter('hello world'));

}
String capitalizeFirstLetter(String str) => str[0].toUpperCase() + str.substring(1);

Output:

Hello world

Another way using string append with interpolation syntax

void main() {
  String message = 'hello world';
  print('${message[0].toUpperCase()}${message.substring(1).toLowerCase()}');
}