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