{

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


Capitalize first letter of a string in Dart

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()}');
}
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

Perl How to: Find a Given Variable String is numeric or not

Dart/Flutter: Check if String is Empty, Null, or Blank example

How to generate Unique Id UUID in Dart or Flutter Programming| Dart or Flutterby Example

How to Sort List of numbers or String in ascending and descending in Dart or Flutter example

Dart Enum comparison operator| Enum compareByIndex example| Flutter By Example

Dart Example - Program to check Leap year or not

Dart Example to count the number of digits in a number| Decimals Count in Dart