The syntax of String.trim() method is:
String trim()
This trim() method of String returns the string without any leading and trailing whitespace.
String.trim() returns value of type String
.
In this example,
str1
with leading and trailing whitespace.trim()
method on str1
to remove the leading and trailing whitespace.void main() {
String str1 = ' Dart ';
String trimmed1 = str1.trim();
print('Trimmed string 1: $trimmed1');
}
Trimmed string 1: Dart
In this example,
str2
with leading and trailing whitespace.trim()
method on str2
to remove the leading and trailing whitespace.void main() {
String str2 = ' Dart ';
String trimmed2 = str2.trim();
print('Trimmed string 2: $trimmed2');
}
Trimmed string 2: Dart
In this example,
str3
with leading whitespace.trim()
method on str3
to remove the leading whitespace.void main() {
String str3 = ' Dart';
String trimmed3 = str3.trim();
print('Trimmed string 3: $trimmed3');
}
Trimmed string 3: Dart
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.