Home > other >  Fluter - decode Unicode when it's a sum of Strings
Fluter - decode Unicode when it's a sum of Strings

Time:07-28

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!

CodePudding user response:

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').

  • Related