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,

  1. We create a string str1 with trailing whitespace.
  2. We use the trimRight() method on str1 to remove the trailing whitespace.
  3. 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,

  1. We create a string str2 with trailing whitespace.
  2. We use the trimRight() method on str2 to remove the trailing whitespace.
  3. 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,

  1. We create a string str3 without trailing whitespace.
  2. When we use the trimRight() method on str3, it does not modify the string.
  3. 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.