Convert all characters in a string to uppercase and lowercase in dart

Introduction :

In this dart programming tutorial, we will learn how to convert all characters of a string to upper-case and to lower-case. The program will read the string as an input from the user. It will then convert all characters to upper case and to lower case.

Dart string class provides two built-in methods for upper-case and lower-case conversion.

Upper case and lowercase conversion in dart :

In this tutorial, we will use the below methods :

1. toUpperCase() → String :

This method is used to convert all characters of a string to uppercase. It returns the modified string.

2. toLowerCase() → String :

This method is used to convert all characters of a string to lowercase. It also returns the modified string.

Example program :

The final dart program looks like as below :

//1
import 'dart:io';

//2
void main() {
  //3
  String mainString;

  //4
  print("Enter a string : ");
  mainString = stdin.readLineSync();

  //5
  print("Changing the string to lowercase : ${mainString.toLowerCase()}");
  print("Changing the string to uppercase : ${mainString.toUpperCase()}");
}

Explanation :

The commented numbers in the above program denote the step numbers below :

  1. We are importing the ‘dart:io’ library for this program. This library is required to read the user input string.
  2. main() is called at the start of the program.
  3. Create one String variable mainString to read the user input value.
  4. Ask the user to enter a string. Read it and store it in mainString variable.
  5. Convert the user input string to all lowercase and uppercase using the toLowerCase() and toUpperCase() methods. Print out the result.

Sample Output :

Enter a string :
Hello World
Changing the string to lowercase : hello world
Changing the string to uppercase : HELLO WORLD

Enter a string :
HELLO WORLD
Changing the string to lowercase : hello world
Changing the string to uppercase : HELLO WORLD

dart uppercase lowercase string