Convert all characters of a string to uppercase or lowercase in dart

Introduction :

In this dart programming tutorial, we will learn how to convert all characters of a string to uppercase or lowercase. To convert all characters of a string to uppercase or lowercase, dart provides two different methods. Let’s take a look at these methods first :

String toLowerCase() :

This method converts all characters in a string to lowercase and returns this new string. If all characters are in lowercase, it returns the same string. This function uses the language independent Unicode mapping and so it works only in some languages.

String toUpperCase() :

This method converts all characters in a string to uppercase and returns this new string. If all characters are in uppercase, it returns the same string. Same as the above function, this function uses the language independent Unicode mapping and so it works only in some languages.

Dart program :

Using the above two methods, we will write one dart program for uppercase and lowercase conversion. Let’s take a look :

import 'dart:io';

main(){
  stdout.writeln("Enter a string : ");
  var str = stdin.readLineSync();

  stdout.writeln("Uppercase conversion : "+str.toUpperCase());
  stdout.writeln("Lowercase conversion : "+str.toLowerCase());
}

Output :

Enter a string :
Hello World !
Uppercase conversion : HELLO WORLD !
Lowercase conversion : hello world !

Enter a string :
this string is in lowercase
Uppercase conversion : THIS STRING IS IN LOWERCASE
Lowercase conversion : this string is in lowercase

Enter a string :
THIS STRING IS IN UPPERCASE
Uppercase conversion : THIS STRING IS IN UPPERCASE
Lowercase conversion : this string is in uppercase

Enter a string :
1234
Uppercase conversion : 1234
Lowercase conversion : 1234

So using toUpperCase() and toLowerCase() methods, we can easily convert a string to uppercase or lowercase. Since these are inbuilt methods of string, we can use them without importing anything.