Strings are immutable; methods return new strings. trim removes outer whitespace, toUpperCase converts case.

Program

Play the program to clean and uppercase a noisy greeting.

string_methods.dart
Replay: real traced execution (multi-file project)
void main() {
  var text = '  Hello World  ';
  var clean = text.trim();
  var upper = clean.toUpperCase();
  print('[$upper]');
}
  1. text ← Hello World

    1void main() {2  var text = '  Hello World  ';3  var clean = text.trim();
    values this step Hello World text
  2. clean ← Hello World

    2var text = '  Hello World  ';3var clean = text.trim();4var upper = clean.toUpperCase();
    values this stepHello Worldclean Hello World text
  3. upper ← HELLO WORLD

    3var clean = text.trim();4var upper = clean.toUpperCase();5print('[$upper]');
    values this stepHELLO WORLDupperHello Worldclean
  4. print('[$upper]');

    4  var upper = clean.toUpperCase();5  print('[$upper]');6}
    output[HELLO WORLD]
    values this stepHELLO WORLDupper

Follow the Methods

  1. text starts with spaces around Hello World.
  2. trim() removes the outer spaces.
  3. clean becomes Hello World.
  4. toUpperCase() changes it to HELLO WORLD.
  5. The program prints [HELLO WORLD]. | step | value | | --- | --- | | original text | Hello World | | after trim() | Hello World | | after toUpperCase() | HELLO WORLD | | output | [HELLO WORLD] |
immutable Each method returns a fresh string; the original is unchanged.
trim Removes leading and trailing whitespace.
toUpperCase Returns an uppercase copy.

Exercise: string_methods.dart

Reproduce [HELLO WORLD], then trace how trim removes the outer spaces before toUpperCase creates HELLO WORLD.