Dart String trim() Method
To remove leading and trailing whitespace from a string in Dart, call the trim() method on the string. It removes whitespace from both ends and returns the remaining text as a new string.
The trim() method removes spaces, tabs, line breaks, and other whitespace characters at the beginning and end of the string. It does not remove whitespace between words.
Syntax of String.trim() in Dart
The syntax of the Dart String.trim() method is:
String.trim()
String.trim() returns a new string with all the leading and trailing white spaces of this string removed.
trim()does not take any arguments.- It returns a new
String. - It does not modify the original string because strings in Dart are immutable.
- Whitespace inside the string remains unchanged.
Dart trim() Examples
Trim Leading and Trailing Spaces from a Dart String
In this example, we will take a string str and trim its edges of whitespaces.
Dart Program
void main(){
String str = ' Hello TutorialKart ';
//trim string
String result = str.trim();
print(result);
}
Output
Hello TutorialKart
The spaces before Hello and after TutorialKart are removed. The space between the two words is preserved.
Trim Tabs and Newline Characters from a Dart String
In this example, we will take a string with spaces, tabs and new line characters at its edges and then use trim() to trim these white characters off.
Dart Program
void main(){
String str = ' \n\n\t \t Hello TutorialKart \t \n ';
//trim string
String result = str.trim();
print(result);
}
Output
Hello TutorialKart
The escape sequences \t and \n represent tab and newline characters. Because they occur at the edges of the string, trim() removes them along with the surrounding spaces.
Difference Between trim(), trimLeft(), and trimRight() in Dart
Dart provides three related methods for removing whitespace from a string:
| Method | Whitespace removed |
|---|---|
trim() | Removes whitespace from both the beginning and end. |
trimLeft() | Removes whitespace only from the beginning. |
trimRight() | Removes whitespace only from the end. |
void main() {
String text = ' Dart string ';
print('>${text.trim()}<');
print('>${text.trimLeft()}<');
print('>${text.trimRight()}<');
}
Output
>Dart string<
>Dart string <
> Dart string<
The angle brackets in the output make the remaining spaces easier to see.
Verify That Dart trim() Returns a New String
Calling trim() does not change the variable that contains the original string. Store the returned value when the trimmed text is needed later.
void main() {
String original = ' Hello Dart ';
String trimmed = original.trim();
print('>$original<');
print('>$trimmed<');
}
Output
> Hello Dart <
>Hello Dart<
Check Whether a Trimmed Dart String Is Empty
A string that contains only whitespace is not empty before trimming. Combine trim() with isEmpty when validating text entered by a user.
void main() {
String input = ' \t\n ';
if (input.trim().isEmpty) {
print('The input contains no visible text.');
}
}
Output
The input contains no visible text.
This check is useful for form fields where an input containing only spaces should be treated as blank.
Remove Extra Whitespace Inside a Dart String
The trim() method removes whitespace only at the edges. To also replace repeated whitespace inside a string with one space, combine trim() with a regular expression.
void main() {
String text = ' Learn Dart\tstring\nmethods ';
String normalized = text.trim().replaceAll(RegExp(r'\s+'), ' ');
print(normalized);
}
Output
Learn Dart string methods
The regular expression \s+ matches one or more consecutive whitespace characters. This operation changes internal spacing, whereas trim() alone does not.
Trim Every String in a Dart List
Use map() when each value in a list must be trimmed. Calling toList() converts the mapped values back into a list.
void main() {
List<String> values = [' apple', 'banana ', ' orange '];
List<String> trimmedValues =
values.map((value) => value.trim()).toList();
print(trimmedValues);
}
Output
[apple, banana, orange]
Dart String trim() FAQs
Does trim() remove spaces between words in Dart?
No. trim() removes whitespace only from the beginning and end of a string. Spaces and other whitespace characters inside the string remain unchanged.
Does Dart trim() modify the original string?
No. Dart strings are immutable. The method returns a new trimmed string, so its return value must be assigned to a variable or used directly.
How do you remove whitespace only from the start of a Dart string?
Use trimLeft(). It removes leading whitespace while preserving whitespace at the end of the string.
How do you remove whitespace only from the end of a Dart string?
Use trimRight(). It removes trailing whitespace while preserving whitespace at the beginning.
Summary of Trimming Strings in Dart
In this Dart Tutorial, we learned how to trim a String using the method String.trim(). Use trimLeft() or trimRight() when whitespace should be removed from only one end. To normalize whitespace inside the string as well, combine trim() with replaceAll() and a suitable regular expression.
TutorialKart.com