How to handle special character in Kotlin Android

Mehdi Satei :

I have a String which I am getting from API (no control over it). When the String contains a special character like an apostrophe, it will be converted to something else.

It looks something like this:

text_view.text = "Hannah's Law"

When displayed on Android, it will be:

Hannah's Law

I tried to convert the String to byteArray and then encode to UTF-8 but no luck:

val byteArray = template.execute(bindingDictionary).toByteArray() // This is the Actual String
String(byteArray,Charsets.UTF_8) // Did not work
kotlin_aj :
'

is HTML for the apostrophe. You can use fromHtml to convert that to text with the apostrophe.

    val fromApi = "Hannah's Law"
    val textFromHtmlFromApi = HtmlCompat.fromHtml(fromApi, HtmlCompat.FROM_HTML_MODE_LEGACY)
    text_view.text = textFromHtmlFromApi

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=175058&siteId=1