OneBite.Dev - Coding blog in a bite size

Convert A String To Uppercase In Dart

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

Dart, an object-oriented programming language, is gaining popularity thanks to its simplicity and efficiency in building high-quality apps. In this article, we will focus on a fundamental topic in Dart programming which involves converting a string to uppercase.

Code snippet for Converting a String to Uppercase in Dart

void main() {
  String example = "this is a string";
  
  print(example.toUpperCase());
}

Through the use of Dart’s built-in function, the above code will translate the text “this is a string” to “THIS IS A STRING”.

Code Explanation for Converting a String to Uppercase in Dart

The built-in function ‘toUpperCase()’ is used to convert a string to uppercase in Dart. Let’s break down how the code works.

  1. Define a String

The first line of the main function defines a string variable named ‘example’. It is assigned the value “this is a string”.

  String example = "this is a string";
  1. Convert the String to Uppercase

The next step is transforming the string to uppercase. This is done with Dart’s built-in function ‘toUpperCase()‘. This function modifies each character of the string, converting lowercase characters to uppercase.

  print(example.toUpperCase());

In this line, ‘example.toUpperCase()’ is called. The toUpperCase() function works on the ‘example’ variable value, changing the string to uppercase. The modified string is then printed to the console.

When this code segment runs, you’ll see that “THIS IS A STRING” is output to the console, showing that the initial lowercase string has been successfully converted to uppercase. This is a primary example of how to change a string to uppercase in Dart, providing the foundation for any actions you might need to take that require uppercase strings.

dart