How to Capitalize the First Letter of String In Flutter ?

· 2 min read
How to Capitalize the First Letter of String In Flutter
How to Capitalize the First Letter of String In Flutter

Sometimes the user needs data in a specific format which is as per user requirement. so in this article, we will go through How to Capitalize the First Letter of String In Flutter?

How to Capitalize the First Letter of String In Flutter?

To capitalize on the first letter of String kindly follow the below instruction.

For example  “this is a string” should give “This is a string“.

extension StringExtension on String {
    String capitalize() {
      return "${this[0].toUpperCase()}${this.substring(1)}";
    }
}

So you can just call your extension like below:

import "string_extension.dart";
var someCapitalizedString = "someString".capitalize();
extension CapExtension on String {
  String get inCaps => '${this[0].toUpperCase()}${this.substring(1)}';
  String get allInCaps => this.toUpperCase();
  String get capitalizeFirstofEach => this.split(" ").map((str) => str.capitalize).join(" ");
}

Usage:

final helloWorld = 'hello world'.inCaps; // 'Hello world'
final helloWorld = 'hello world'.allInCaps; // 'HELLO WORLD'
final helloWorld = 'hello world'.capitalizeFirstofEach; // 'Hel

Users can also try the below things.

void main() {
  print(capitalize("this is a string"));
  // displays "This is a string"
}

String capitalize(String s) => s[0].toUpperCase() + s.substring(1);

Import intl package like below:

import 'package:intl/intl.dart';
String sentence = toBeginningOfSentenceCase('this is a string');

Users can also try the below ways:

String capitalize(String s) => (s != null && s.length > 1)
    ? s[0].toUpperCase() + s.substring(1)
    : s != null ? s.toUpperCase() : null;

Conclusion:

In this article, we have been through How to make Capitale a First Letter of String In Flutter?

Still, need support for Flutter Development?

Keep Fluttering !!!
Keep Learning !!!

FlutterAgency.com is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget GuideFlutter ProjectsCode libs and etc.

FlutterAgency.com is one of the most popular online portal dedicated to Flutter Technology and daily thousands of unique visitors come to this portal to enhance their knowledge on Flutter.

Leave a Reply