Capitalized the First Letter of Every Word in the String in Flutter
Last Updated :
24 Apr, 2025
With the help of the extension method, you can easily capitalize the first letter of every word in a string in your Dart or Flutter code. we first define an extension method called capitalize() on the String class. This method capitalizes the first letter of the string by converting the first character to uppercase using the toUpperCase() method, and then concatenating it with the rest of the string using the substring() method.
How to Use?
Dart
String myString = "hello world";
String capitalizedString = myString.split(' ').map((word) => word.capitalize()).join(' ');
// "Hello World"
print(capitalizedString);
- String myString = "hello world" - Declares a string variable called myString with the value "hello world".
- String capitalizedString = myString.split(' ').map((word) => word.capitalize()).join(' '); - Splits the myString variable into individual words using the split() method with a space delimiter, maps over the resulting list of words using the map() method with an anonymous function that calls the capitalize() method (not defined yet), and then joins the modified words back together with a space delimiter using the join() method. The resulting string with the capitalized words is assigned to the capitalizedString variable.
- print(capitalizedString); // "Hello World" - Prints the capitalized string to the console.
Define the capitalize() Method
Dart
extension StringExtensions on String {
String capitalize() {
return "${this[0].toUpperCase()}${this.substring(1)}";
}
}
Step By Step Implementation
Step 1: Create a New Project in Android Studio or in vs code
To set up Flutter Development on Android Studio please refer to Android Studio Setup for Flutter Development, and then create a new project in Android Studio please refer to Creating a Simple Application in Flutter.
Step 2: Import the material package
A material package gives us the essential functions and Parameters, now call the runApp method that needs an Application in the main function.
import 'package:flutter/material.dart';
void main() {
runApp(RunMyApp());
}
In the above code, runApp method calls the class RunMyApp, Now we have to create it.
Step 3: Creating Stateless Widget
Now we have to make a stateless widget because our application does not go to change its state and then return the MaterialApp widget which allows us the set the title and theme and many more of the application.
Shortcut: For creating a stateless or Stateful widget, you can create a stateless or stateful widget by just typing three alphabets ‘stl’ and you can see a stateless widget and then hit enter.
class RunMyApp extends StatelessWidget {
const RunMyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp();
}
}
Step 4: Working with Scaffold Widget
Give the home property and there can be a scaffold widget with AppBar and body property. AppBar allows us to give the title of AppBar, color, leading, and trailing icon.
home: Scaffold(
appBar: AppBar(
title: Text('Appbar'),
),
body:Â
),
Step 5: Display the string in the body of the app
Finally, we create a Text widget that displays the capitalizedString and originalString variable in the center of the screen using a TextStyle with a font size of 20 and FontWeight.w200.
Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Original String - ${myString}',
style: TextStyle(fontWeight: FontWeight.w200, fontSize: 20),
),
Text(
'Capitalized String - ${capitalizedString}',
style: TextStyle(fontWeight: FontWeight.w200, fontSize: 20),
)
],
),
),
We are using the center widget, further, we use the Column widget as a child, and then using the text widget we show the strings.
Code Example
Dart
import 'package:flutter/material.dart';
void main() {
runApp(RunMyApp());
}
class RunMyApp extends StatelessWidget {
RunMyApp({super.key});
@override
Widget build(BuildContext context) {
String myString = "Capitalized the first letter of every word the string";
String capitalizedString =
myString.split(' ').map((word) => word.capitalize()).join(' ');
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(primarySwatch: Colors.green),
home: Scaffold(
appBar: AppBar(
title:
Text('Capitalized the First letter of every word of the string'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Original String - ${myString}',
style: TextStyle(fontWeight: FontWeight.w200, fontSize: 20),
),
Text(
'Capitalized String - ${capitalizedString}',
style: TextStyle(fontWeight: FontWeight.w200, fontSize: 20),
)
],
),
),
),
);
}
}
extension StringExtensions on String {
String capitalize() {
return "${this[0].toUpperCase()}${this.substring(1)}";
}
}
OutputÂ
Â
Similar Reads
How to Capitalize the First Letter of a String in Flutter?
In Flutter we can create the extension and capitalize the first letter of the string. This is an extension method that extends the String class in Dart, providing a new method called capitalize(). The capitalize() method takes a string, capitalizes the first letter, and returns the modified string.
4 min read
How to Find the Length of a String in Dart?
To find the length of a string in the dart, we make use of the length property of a string class in Dart. This method returns the length of the string, which is an integer.Syntax: string_name.lengthReturn Type: IntegerImage Representation: Find the length of the stringExample: Dart// Main function m
1 min read
Flutter - Convert an Image into Base64 String
Base64 encoding is used to convert bytes that have binary or text data into ASCII characters. Encoding prevents the data from getting corrupted when it is transferred or processed through a text-only system. In this article, we will discuss how to convert an image into Base64 String. A sample video
4 min read
Custom Label Text in TextFormField in Flutter
Today we will explore how to make a custom label above the text field whose style will change according to your tap on the text field/text form field. So that user can easily understand which text field is currently active. Example: In this, there are two fields, the first is email and the second is
5 min read
Flutter Quill - Rich Text Editor in Flutter Application
This post will explain how to integrate a rich text editor into your Flutter application so that it may enable rich text editing. There are many more things we can do with Flutter Quill, but they can get a little involved. The example below is a pretty basic demonstration of what we mean. Let's firs
5 min read
Flutter - Restrict TextField to Input Special Characters
In Flutter, user input validation is essential for maintaining data integrity and ensuring a smooth user experience. One common requirement is to restrict the input of special characters in a TextField to prevent unwanted characters in certain fields, such as usernames or any other text-based input.
3 min read
Flutter - Prefix and Suffix Icon in TextField
In this article, we will implement how to add prefix and Suffix icons in the TextField. A sample image is given below to get an idea about what we are going to do in this article. Â Step by Step Implementation Step 1: Create a New Project in Android Studio To set up Flutter Development on Android Stu
2 min read
Flutter - How to Change App and Launcher Title in Different Platforms
Sometimes you notice that we have to change the App title in Android, and iOS apps, and the title for flutter web during or after loading. So in this article, we will cover all the places where we have to change the app title with different platforms. 1. Android App Title Change the android label na
2 min read
Autofill Hints Suggestion List in Flutter
You must have noticed that the majority of websites and mobile applications provide an autofill list. These features can now be added to your Flutter app without importing any packages. Just read the article for this amazing property in the text form field/text field. Example: We need to display sev
2 min read
Flutter - Extract Data From an Image to Text
Google ML kit provides many features, one of them is Image to Text Extraction, through which we can simply extract text from an image. To extract text from the image first we need to take an image as input either from the gallery or camera for which we will use 'image_picker: ^1.0.4' (dependency fro
3 min read