Strings
String Methods
trim and toUpperCase
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]');
}
text ← Hello World
1void main() {2 var text = ' Hello World ';3 var clean = text.trim();values this step Hello World textclean ← Hello World
2var text = ' Hello World ';3var clean = text.trim();4var upper = clean.toUpperCase();values this stepHello Worldclean Hello World textupper ← HELLO WORLD
3var clean = text.trim();4var upper = clean.toUpperCase();5print('[$upper]');values this stepHELLO WORLDupperHello Worldcleanprint('[$upper]');
4 var upper = clean.toUpperCase();5 print('[$upper]');6}output[HELLO WORLD]values this stepHELLO WORLDupper
Follow the Methods
textstarts with spaces aroundHello World.trim()removes the outer spaces.cleanbecomesHello World.toUpperCase()changes it toHELLO WORLD.- The program prints
[HELLO WORLD]. | step | value | | --- | --- | | original text |Hello World| | aftertrim()| Hello World | | aftertoUpperCase()| 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.