Enrico's Ricardo
Enrico's Ricardo

Reputation: 59

how to convert charCode to uncode in scala

I am trying to convert string \u8fc8 to "迈" in scala. The string \u8fc8 is stored as six chars which are '\', 'u', '8', 'f', 'c', '8'.

In Python I can do this:

print("\u8fc8".decode("unicode-escape"))

It prints "迈", but I could not find a way to make that happen in Scala, can any body help me?

Upvotes: 0

Views: 161

Answers (1)

pme
pme

Reputation: 14803

"\u8fc8".format("unicode-escape")

Check this in ScalaFiddle: how to convert charCode to uncode in scala

For "\u8fc8" it is a bit more complex, with the answer of scala-unescape-unicode-string-without-apache:

def unescapeUnicode(str: String): String = 
  """\\u([0-9a-fA-F]{4})""".r.replaceAllIn(str, 
    m => Integer.parseInt(m.group(1), 16).toChar.toString)

unescapeUnicode("\u8fc8")

Check the updated ScalaFiddle: how to convert charCode to uncode in scala

Upvotes: 1

Related Questions