What is the String.toLowerCase method in Dart?

Overview

The toLowerCase method converts all the characters in a string to lowercase and returns the converted string as a result. However, it doesn’t change the actual string.

Note: If the string is already in lowercase, then the string is returned as it is.

Syntax

String toLowerCase()

Argument

This method doesn’t take any arguments.

Return value

This method converts all the characters present in the current string to lowercase and returns it.

Code

The code given below shows us how to use the toLowerCase method:

Using the toLowerCase method
void main() {
// create a empty string
var str = "TeST";
print('The string is - $str.');
// Convert the string to lowercase
print("str.toLowerCase() : ${str.toLowerCase()}");
str = "HeLLO";
print('\nThe string is - $str.');
// Convert the string to lowercase
print("str.toLowerCase() : ${str.toLowerCase()}");
}

Explanation

In the code above:

  • In line 3, we create a variable with the name str and assign a "TeST" string as a value.

  • In line 7, we use the toLowerCase method to convert the str to a string with lowercase characters only. The toLowerCase method will return test as a result.

  • In line 9, we change the value of the str variable to the "HeLLO" string.

  • In line 12, we use the toLowerCase method to convert the str to a string with all lowercase characters. The toLowerCase method will return hello as a result.

Free Resources