The Beauty of Dart String Methods

Motabar Javaid
3 min readAug 25, 2021

Dart is a Google’s client-optimized Object Oriented Programming Language which is used to build User Interfaces in conjunction with Flutter — A Cross Platform Development Toolkit.

Dart is a statically typed language that offers features like Sound Null Safety , Spread operators, Collection if’s for its Collection class and the powerful Dart VM that enables it to have its renowned Hot Reload functionality.

Like any other Programming Languages, Dart offers String as types. It can be represented with either a single, double or triple quotes.

What makes Dart Strings special is the Methods that are available for the String Types. Here are some which comes in very handy when dealing with String literals.

endsWith()

The endsWith Method takes a String as an argument and returns true if the String on which it is called ends at the given parameter.Its worth mentioning, the endsWith Method is case sensitive. Let’s see that in practice:

trim()

Have you ever felt lazy while removing trailing or leading white spaces from large Strings? Dart’s trim Method got you covered. When called, it returns a new String with no whitespaces. Here’s the code:

replaceAll()

The replaceAll function on Strings takes two string parameters: First one is the substring that is to be replaced and the second one is the one with which it is to be replaced with. The function replaces the string with the substring at all the instances within that String. Confused? Let’s make it easier by looking at an example:

compareTo()

We’ll all been there at some points where we want to compare two Strings but just can’t do it with the ‘==’ operator. Why can’t we do that, putting it in simpler words is because the equality operator doesn’t compare the exact literals but the Object references. So what’s the solution? Here comes compareTo() method to the rescue. The Method takes in two parameters: The first one being the one you want to compare and the second one, you guessed it right, the one to which we want to compare our String. The function has three return values based on the result:

0 : When the two Strings match.
1: When the first string is greater than the second (based on ASCII values).
-1: When the first string is smaller than the second string.

Let’s see its functionality by looking at an example:

toLowerCase() & toUpperCase()

The toLowerCase() and toUpperCase() methods are self explanatory. The former convert the string literal into Lower-case while the latter does the opposite and converted the string into Upper-case characters. Here’s an example containing both:

split()

The split() method splits up the string by taking a parameter as a delimeter and returns back the result in the form of List of splitted substrings. Let’s look at a code example for it:

substring()

The substring() method on dart String literals provides us with the functionality of retreiving a substring by its position. It takes in two parameters: The starting index (inclusive )and the ending index (exclusive). The second parameter is optional. The subString() method in code works as follows:

toString()

Last but not the least, probably the one any developer would be mostly using is the toString() method. The sole purpose of this method is to return the string representaion of the object on which it is called. Let’s see it in action:

That’s all for now Folks! Thanks for reading this article ❤️

Clap 👏 If this article helped you.

Feel free to post any queries or corrections you think are required…✔
Do leave a feedback so I can improve my content. Thankyou! 😃

--

--

Motabar Javaid

I try to teach what I learn. Flutter Developer | Dart Enthusiast