Dart String trim()
Syntax & Examples
Syntax of String.trim()
The syntax of String.trim() method is:
String trim()
This trim() method of String returns the string without any leading and trailing whitespace.
Return Type
String.trim() returns value of type String
.
✐ Examples
1 Trim leading and trailing whitespace
In this example,
- We create a string
str1
with leading and trailing whitespace. - We use the
trim()
method onstr1
to remove the leading and trailing whitespace. - We print the trimmed string to standard output.
Dart Program
void main() {
String str1 = ' Dart ';
String trimmed1 = str1.trim();
print('Trimmed string 1: $trimmed1');
}
Output
Trimmed string 1: Dart
2 Trim leading and trailing whitespace
In this example,
- We create a string
str2
with leading and trailing whitespace. - We use the
trim()
method onstr2
to remove the leading and trailing whitespace. - We print the trimmed string to standard output.
Dart Program
void main() {
String str2 = ' Dart ';
String trimmed2 = str2.trim();
print('Trimmed string 2: $trimmed2');
}
Output
Trimmed string 2: Dart
3 Trim leading whitespace
In this example,
- We create a string
str3
with leading whitespace. - We use the
trim()
method onstr3
to remove the leading whitespace. - We print the trimmed string to standard output.
Dart Program
void main() {
String str3 = ' Dart';
String trimmed3 = str3.trim();
print('Trimmed string 3: $trimmed3');
}
Output
Trimmed string 3: Dart
Summary
In this Dart tutorial, we learned about trim() method of String: the syntax and few working examples with output and detailed explanation for each example.