Shailen Tuli's blog

Thoughts on coding in Dart, Ruby, Python and Javascript

Character Codes and Strings in Dart

| Comments

In my Python programs, I found use for the ord() and chr() builtins to convert between string characters and their ASCII representations. Similar tools exist in Dart. To get a list of character codes for a string, use charCodes:

String s = "hello";
print(s.charCodes);
// [104, 101, 108, 108, 111]

To get a specific character code, you can either subscript charCodes:

print(s.charCodes[0]); 

or - this is the more common way - use charCodeAt:

print(s.charCodeAt(0));

To assemble a string from a list of character codes, use the String factory, fromCharCodes:

List<int> charCodes = [104, 101, 108, 108, 111];
print(new String.fromCharCodes(charCodes));
// "hello"

If you are using a StringBuffer to build up a string, you can add individual charCodes using addCharCode (use add() to add characters; use addCharCode() to add charCodes):

StringBuffer sb = new StringBuffer();
charCodes.forEach((charCode) {
  sb.addCharCode(charCode);
});

print(sb.toString());
// "Hello" 

Here is an implementation of the rot13 algorithm, using the tools described above. rot13 is a simple letter substitution algorithm that rotates a string by 13 places by replacing each character in it by one that is 13 characters removed (‘a’ becomes ‘n’, ‘N’ becomes ‘A’, etc.):

String rot13(String s) {
  List<int> rotated = [];

  s.charCodes.forEach((charCode) {
    final int numLetters = 26;
    final int A = 65;
    final int a = 97;
    final int Z = A + numLetters;
    final int z = a + numLetters;

    if (charCode < A ||
        charCode > z ||
        charCode > Z && charCode < a) {
      rotated.add(charCode);
    }
    else {
      if ([A, a].some((item){
        return item <= charCode && charCode < item + 13;
      })) {
        rotated.add(charCode + 13);
      } else {
        rotated.add(charCode - 13);
      }   
    }
  });

  return (new String.fromCharCodes(rotated));
}

Running the code:

var wordsList = [["Jung", "be", "purely", "barf"],
                  ["aha", "nun"]];
wordsList.forEach((word_list) {
  print(word_list.map((word) {
    return rot13(word);
  }));
});
// ["What", "or", "cheryl", "ones"]
// ["nun", "aha"] 

and:

String str = "aMz###AmZ";
assert(rot13(rot13(str)) == str);

Comments