The `takeWhile` method in Dart returns a lazy iterable of the leading elements of the original iterable that satisfy a specified test function.
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
.
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. |
Runes.takeWhile() returns value of type Iterable<int>
.
In this example,
runes
from the string '123Hello'.takeWhile
method with a test function to take all digits (ASCII values less than 58).void main() {
Runes runes = Runes('123Hello');
Iterable<int> taken = runes.takeWhile((element) => element < 58);
print('Taken digits: $taken');
}
Taken digits: (49, 50, 51)
In this example,
characters
from the string 'abc123'.takeWhile
method with a test function to take all characters with ASCII values less than 97 (lowercase letters).void main() {
Runes characters = Runes('abc123');
Iterable<int> takenChars = characters.takeWhile((element) => element < 97);
print('Taken characters: $takenChars');
}
Taken characters: (97, 98, 99)
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.