首页 > 解决方案 > 如何在 Kotlin Android 中处理特殊字符

问题描述

我有一个从 API 获得的字符串(无法控制它)。当字符串包含像撇号这样的特殊字符时,它将被转换为其他字符。

它看起来像这样:

text_view.text = "Hannah's Law"

在 Android 上显示时,它将是:

Hannah's Law

我尝试将字符串转换为 byteArray,然后编码为 UTF-8,但没有运气:

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

标签: androidkotlin

解决方案


例如,请使用 unicode 符号 ,例如https://www.rapidtables.com/code/text/unicode-characters.html

String str = "Hannah's Law"  

采用

String str = "Hannah\u0027s Law"

如果您需要例如字符串末尾的空格,则同样的事情

String str = "string with space in the end\u0020"

供 Kotlin 使用

var str: String = "string with space in the end\u0020"

推荐阅读