我在 UILabel 中显示了一个字符串,它似乎有几个换行符。
这是原始 JSON 数据的链接(文本位于关键字“flavor_text
”中):http://pokeapi.co/api/v2/pokemon-species/1/
这是字符串的样子:
{
"flavor_text":"Bulbasaur can be seen napping in bright sunlight.\nThere is a seed on its back. By soaking up the sun\u2019s rays,\nthe seed grows progressively larger."
}
我试过使用以下修整代码:
myUILabel.text = flavorText.trimmingCharacters(in: .whitespacesAndNewlines)
但换行符仍然存在。有什么建议吗?
最佳答案
您是否尝试过 replacingOccurrences
。
myUILabel.text = flavorText.replacingOccurrences(of target: "\n", with replacement: "")
关于ios - 来自 Web 请求 JSON 的 Swift 字符串有换行符 - 如何删除换行符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39543947/