OneBite.Dev - Coding blog in a bite size

Convert A String To Lowercase In Dart

Code snippet for how to Convert A String To Lowercase In Dart with sample and detail explanation

Since its inception, Dart has grown in popularity due to its simplicity and effectiveness in programming, particularly when used in Google’s Flutter framework for building natively compiled applications. One common task in programming is converting a string to lowercase and in this article, we will explore how to perform this in Dart.

Code snippet to convert a string to lowercase in Dart

Here is a simple code snippet for implementing this:

void main() { 
    String my_str = 'DART STRING TO LOWERCASE';
    print(my_str.toLowerCase());
} 

When the above code is executed, it will return the output as ‘dart string to lowercase’.

Code Explanation for Converting a String to Lower Case in Dart

Let’s explain this process step by step:

  1. We start the code by declaring a main() function which is the entry point of our Dart application.
void main() { 
  1. Inside this function, we declare a String variable my_str and assign the text 'DART STRING TO LOWERCASE' to it.
 String my_str = 'DART STRING TO LOWERCASE';
  1. The toLowerCase() function is a built-in method in Dart for converting all the characters in a string to lower case.
 my_str.toLowerCase();
  1. We print this converted string to the console using the print() function.
print(my_str.toLowerCase());
  1. In the end, the code is closed with two closing brackets.

Hence, by simply calling the toLowerCase() function on a string, we’re able to convert the entire string to lowercase characters in Dart. Remember, this method does not change the original string, rather it returns a new string in which all the characters are in lowercase.

This function can come in pretty handy especially when performing operations such as string comparisons where case sensitivity might play an undesired role.

dart