Dart – Iterate over Characters of a String

To iterate over a string, character by character, call runes on given string which returns a Runes object. Use forEach() method on this Runes object, which lets us iterate over each code point in the string, where code point is a character.

Syntax

The syntax to iterate over characters of a string str is

</>
Copy
str.runes.forEach((c) {
  var ch = new String.fromCharCode(c);
  //code
});

Example

In this example, we take a string 'apple' with iterate over the characters of this string.

main.dart

</>
Copy
void main() {
  var str = 'apple';
  str.runes.forEach((c) {
    var ch = new String.fromCharCode(c);
    print(ch);
  });
}

Output

a
p
p
l
e

Conclusion

In this Dart Tutorial, we learned how to iterate over characters of a given string, with examples.