dart

Dart program to capitalize the first letter of a string

In a Dart program, String can be converted to uppercase or lowercase using inbuilt functions toUpperCase() and toLowerCase(). If you want to capitalize the first letter of the Dart String then you can use the below code.

void main() async {
  String myStr = "devsheet";
  
  String result = "${myStr[0].toUpperCase()}${myStr.substring(1).toLowerCase()}";
  
  print(result);
}
void main() async {
  String result = capitalize("devsheet");
  
  print(result);
}

String capitalize(String str) => str[0].toUpperCase() + str.substring(1);
Here, we have created a capitalize function that will return the String with the first letter in uppercase format and the rest of the letters in lowercase.
Was this helpful?