我有一个我在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)
但换行仍然存在。有什么建议吗?
答案 0 :(得分:2)
您是否尝试使用replacingOccurrences
。
myUILabel.text = flavorText.replacingOccurrences(of target: "\n", with replacement: "")