Menu
Coddy logo textTech

Basic String Methods

Part of the Fundamentals section of Coddy's Dart journey — lesson 41 of 94.

String methods allow you to transform strings in various ways. Three common methods are toLowerCase(), toUpperCase(), and trim().

Create a string with mixed case:

String message = "  Hello Dart  ";

Convert to lowercase using toLowerCase():

String lowercase = message.toLowerCase();
print(lowercase);

After executing the above code, the output will be:

  hello dart  

Convert to uppercase using toUpperCase():

String uppercase = message.toUpperCase();
print(uppercase);

After executing the above code, the output will be:

  HELLO DART  

Remove leading and trailing whitespace using trim():

String trimmed = message.trim();
print(trimmed);

After executing the above code, the output will be:

Hello Dart
challenge icon

Challenge

Beginner

In this challenge, you'll work with basic string methods in Dart: toLowerCase(), toUpperCase(), and trim().

Your task is to create a program that:

  1. Reads a string input
  2. Creates and prints three modified versions of the input string:
    • Convert the string to all lowercase using toLowerCase()
    • Convert the string to all uppercase using toUpperCase()
    • Remove any leading and trailing whitespace using trim()

For example, if the input is " Hello Dart! ", your program should output:

Original: "  Hello Dart!  "  
Lowercase: "  hello dart!  "
Uppercase: "  HELLO DART!  "
Trimmed: "Hello Dart!"

Notice how toLowerCase() and toUpperCase() change the case of letters but preserve spaces, while trim() removes spaces at the beginning and end but keeps the text unchanged.

Cheat sheet

String methods allow you to transform strings in various ways:

toLowerCase() - converts string to lowercase:

String message = "Hello Dart";
String lowercase = message.toLowerCase();
print(lowercase); // hello dart

toUpperCase() - converts string to uppercase:

String uppercase = message.toUpperCase();
print(uppercase); // HELLO DART

trim() - removes leading and trailing whitespace:

String message = "  Hello Dart  ";
String trimmed = message.trim();
print(trimmed); // Hello Dart

Try it yourself

import 'dart:io';

void main() {
  // Read input string
  String input = stdin.readLineSync()!;
  
  // Print the original string
  print('Original: "$input"');
  
  // TODO: Convert the input to lowercase and print it
  
  // TODO: Convert the input to uppercase and print it
  
  // TODO: Trim the input and print it
  
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals