{

How to capitalize the first letter of a string in dart?| Flutter By Example


parse String to or from Integer in Dart

In these examples, Learn how to capitalize the first letter of a string or statement in Dart programming with 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, Written capitalizeFirstLetter function

  • takes input as String and return 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()}');
}
THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.





Related posts

Dart| Flutter How to: Read pubspec.yaml attributes (version) with examples

How to Create and build a Singleton Class Dart or Flutter example| Dart By Example

What is the difference between var and dynamic and final in dart?| Flutter By Example

How to: Check if Phone number is valid or not in Dart| Flutter By Example

Dart| Flutter How to read an image from a disk and resize a file

Dart/Flutter: How to write setter and getter fields or members variables in a class with Example

Dart| Flutter How to get extension name and MIME type of a file with example