Dart Runes takeWhile()
Syntax & Examples
Runes.takeWhile() method
The `takeWhile` method in Dart returns a lazy iterable of the leading elements of the original iterable that satisfy a specified test function.
Syntax of Runes.takeWhile()
The syntax of Runes.takeWhile() method is:
Iterable<int> takeWhile(bool test(int value))
This takeWhile() method of Runes returns a lazy iterable of the leading elements satisfying test
.
Parameters
Parameter | Optional/Required | Description |
---|---|---|
test | required | A function that takes an integer element as input and returns a boolean value indicating whether the element satisfies the test. |
Return Type
Runes.takeWhile() returns value of type Iterable<int>
.
✐ Examples
1 Take digits
In this example,
- We create a sequence of Unicode code points
runes
from the string '123Hello'. - We use the
takeWhile
method with a test function to take all digits (ASCII values less than 58). - We then print the taken digits to standard output.
Dart Program
void main() {
Runes runes = Runes('123Hello');
Iterable<int> taken = runes.takeWhile((element) => element < 58);
print('Taken digits: $taken');
}
Output
Taken digits: (49, 50, 51)
2 Take characters less than 'a'
In this example,
- We create a sequence of Unicode code points
characters
from the string 'abc123'. - We use the
takeWhile
method with a test function to take all characters with ASCII values less than 97 (lowercase letters). - We then print the taken characters to standard output.
Dart Program
void main() {
Runes characters = Runes('abc123');
Iterable<int> takenChars = characters.takeWhile((element) => element < 97);
print('Taken characters: $takenChars');
}
Output
Taken characters: (97, 98, 99)
Summary
In this Dart tutorial, we learned about takeWhile() method of Runes: the syntax and few working examples with output and detailed explanation for each example.