Format String to Title Case Using Dart Extensions

Parth Samnani
3 min readApr 16, 2021

Problem

I came across this problem while working on a content-centric application where I wanted to show content names in Title Case.

After scouring the internet for existing Dart packages to solve my problem, I realized that no proper solution exists.

There are many packages which help you deal with sentence casing but they don’t consider conjuctions (— e.g. and, or, but, so), prepositions (— e.g. of, on, as, at,) articles (— e.g. a, an) etc.

Solution

Title case a string in dart using extensions.

1. Capitalize the first letter of all words. eg. Flutter Is a Cool Language

2. Lowercase all other letters. eg. Flutter Is a Cool Language

3. Exclude words like -> a, an, or, of, and etc. Flutter Is a Cool Language

Extensions in Dart

Extension methods were introduced in Dart 2.7. They allow you to add functionality to existing libraries. Extensions can define operators, getters, setters and existing methods.

To learn more about extensions, checkout the Dart documentation.

Note : For extensions to work, set the minimum version for Dart SDK to 2.7 in your pubspec.yaml file

environment:
sdk: ">=2.7.0 <3.0.0"

To apply them in our use case, we create a file → string_extension.dart and define an extension called TitleCase on the Dart String class.

extension TitleCase on String {}

We keep a track of words which should not be capitalized while Title Casing a String. Let us call that list exceptions.

List<String> exceptions =['a','abaft','about','above','afore','after','along','amid','among','an','apud','as','aside','at','atop','below','but','by','circa','down','for','from','given','in','into','lest','like','mid','midst','minus','near','next','of','off','on','onto','out','over','pace','past','per','plus','pro','qua','round','sans','save','since','than','thru','till','times','to','under','until','unto','up','upon','via','vice','with','worth','the","and','nor','or','yet','so'];

Now, we define a method called toTitleCase in this extension where we handle the logic to Title Case a string.

Let us try to Title Case the following strings ->

String randomCase = “This IS an examPLE InPuT STRING to SHow the implementATIon of Title CasING a string IN randOM CaSe”;

String upperCase = "FLUTTER IS AN OPEN-SOURCE UI SOFTWARE DEVELOPMENT KIT CREATED BY GOOGLE. IT IS USED TO DEVELOP APPLICATIONS FOR ANDROID, IOS, LINUX, MAC, WINDOWS, GOOGLE FUCHSIA, AND THE WEB FROM A SINGLE CODEBASE.";

Now, in order to use the extension we created, Import string_extensions.dart in the file where you wish to use toTitleCase method and implement it as the following

import 'string_extensions.dart';void titleCaseExample(){
print(randomCase.toTitleCase());
print(upperCase.toTitleCase());
}

If you run your code, you get the following output ->

This Is an Example Input String to Show the Implementation of Title Casing a String in Random Case. Flutter Is an Open Source Ui Software Development Kit Created by Google. It Is Used to Develop Applications for Android, Ios, Linux, Mac, Windows, Google Fuchsia, and the Web from a Single Codebase.

One perk of using extensions is that if you are using an IDE like Android Studio for development, the extension method will show up in the code auto completer.

Check out the dartpad playground if you’d like to play with this code online

--

--