android - 如何在 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
解决方案
例如,请使用 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"
推荐阅读
- javascript - 如何在将表字段导出到 csv 时忽略逗号?
- typescript - TypeScript 动态生成值类型检查
- django - Django 优先队列
- android - 如何在android按钮中添加阴影?
- javascript - React ref 在循环内重新渲染时中断
- kubernetes - Kubernetes - 如何通过休息请求从 pod 中的 Web 服务器访问服务
- javascript - Flatlist 从项目中获取价值
- node.js - MochaJS:被拒绝的承诺不会触发失败的测试
- javascript - 从 XML 对象获取地址到 NetSuite 中的变量
- javascript - 如何在 Chrome 开发工具中的元素属性下编辑元素