Dart String trimLeft()
Syntax & Examples
Syntax of String.trimLeft()
The syntax of String.trimLeft() method is:
String trimLeft()
This trimLeft() method of String returns the string without any leading whitespace.
Return Type
String.trimLeft() returns value of type String
.
✐ Examples
1 Trim leading whitespace from a string
In this example,
- We create a string
str
with leading whitespace ' Hello, World!'. - We then use the
trimLeft()
method to remove any leading whitespace from the string. - The trimmed string is stored in
trimmedStr
. - We print both the original and trimmed strings to standard output.
Dart Program
void main() {
String str = ' Hello, World!';
String trimmedStr = str.trimLeft();
print('Original String: $str');
print('Trimmed String: $trimmedStr');
}
Output
Original String: Hello, World! Trimmed String: Hello, World!
2 Trim leading whitespace from another string
In this example,
- We create a string
str
with leading whitespace ' ABCDEF'. - We then use the
trimLeft()
method to remove any leading whitespace from the string. - The trimmed string is stored in
trimmedStr
. - We print both the original and trimmed strings to standard output.
Dart Program
void main() {
String str = ' ABCDEF';
String trimmedStr = str.trimLeft();
print('Original String: $str');
print('Trimmed String: $trimmedStr');
}
Output
Original String: ABCDEF Trimmed String: ABCDEF
3 Trim leading whitespace from a sentence
In this example,
- We create a string
str
with leading whitespace ' Lorem ipsum dolor sit amet'. - We then use the
trimLeft()
method to remove any leading whitespace from the string. - The trimmed string is stored in
trimmedStr
. - We print both the original and trimmed strings to standard output.
Dart Program
void main() {
String str = ' Lorem ipsum dolor sit amet';
String trimmedStr = str.trimLeft();
print('Original String: $str');
print('Trimmed String: $trimmedStr');
}
Output
Original String: Lorem ipsum dolor sit amet Trimmed String: Lorem ipsum dolor sit amet
Summary
In this Dart tutorial, we learned about trimLeft() method of String: the syntax and few working examples with output and detailed explanation for each example.