Search code examples
stringflutterunicodesum

Decode Unicode when it's a sum of Strings


I have a Unicode String as a sum of 6 substrings:

String unicode = '\\' + 'u' + '0' + '0' + 'e' + '4';

So is there any chance in flutter to decode it into a unicode String like that:

String unicodeString = '\u00e4';

Thanks a lot for your helping!


Solution

  • Yes, this can be easily done:

    • extract the hexadecimal number after \u
    • convert it to an integer, resulting in a Unicode codepoint
    • convert the codepoint to a string
    String unicode = '\\u00e4';
    var codepoint = int.parse(unicode.substring(2, 6), radix: 16);
    String unicodeString = String.fromCharCode(codepoint);
    print(unicodeString);
    

    Also note that you can just write '\\u00e4'. As it starts with a double backslash, this is indeed a string with 6 characters (and not equal to '\u00e4').