How to work with char types in Dart? (Print alphabet)

Dart does not have character types.

To convert a code point to a string, you use the String constructor String.fromCharCode:

int c = "a".codeUnitAt(0);
int end = "z".codeUnitAt(0);
while (c <= end) {
  print(String.fromCharCode(c));
  c++;
}

For simple stuff like this, I'd use "print" instead of "stdout", if you don't mind the newlines.

There is also:

int char_a = 'a'.codeUnitAt(0);
print(String.fromCharCodes(new Iterable.generate(26, (x) => char_a + x)));

or, using newer list literal syntax:

int char_a = 'a'.codeUnitAt(0);
int char_z = 'z'.codeUnitAt(0);
print(String.fromCharCodes([for (var i = char_a; i <= char_z; i++) i]));

As I was finalizing my post and rephrasing my question’s title, I am no longer barking up the wrong tree thanks to this question about stdout.

It seems that one proper way of writing characters is to use stdout.writeCharCode from the dart:io library.

import 'dart:io';

void  ft_print_alphabet()
{
  var c = "a".codeUnits.first;

  while (c <= "z".codeUnits.first)
    stdout.writeCharCode(c++);
}

void main() {
  ft_print_alphabet();
}

I still have no clue about how to manipulate character types, but at least I can print them.

Tags:

Dart