Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate over a String, char by char, in Dart?

I don't see Dart strings treated as lists of characters. I assume I have to use for loops, which would be lame.

like image 572
mcandre Avatar asked Feb 15 '12 01:02

mcandre


People also ask

How do you iterate through a Dart 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.

How do you find the characters in a Dart string?

To find the substring of a string in Dart, call substring() method on the String and pass the starting position and ending position of the substring in this string, as arguments.

How do you check if a string contains a substring in Dart?

To check if a string contains other string in Dart, call contains() method on this string and pass the other string as argument. contains() method returns returns a boolean value of true if this string contains the other string, or false if the this string does not contain other string.

What is rune in Dart?

A rune is an integer representing a Unicode code point. The String class in the dart:core library provides mechanisms to access runes. String code units / runes can be accessed in three ways − Using String.codeUnitAt() function.


1 Answers

An alternative implementation (working with characters outside the basic multilingual plane):

"A string".runes.forEach((int rune) {   var character=new String.fromCharCode(rune);   print(character); }); 
like image 153
CedX Avatar answered Sep 27 '22 22:09

CedX