Eman H Eman H - 2 months ago 16
iOS Question

Swift string from web request JSON has line breaks - how to remove line breaks?

I have a String I am displaying in a UILabel and it has what seems to be several line breaks.

Here is the link to the raw JSON data (text is in key "

flavor_text
"): http://pokeapi.co/api/v2/pokemon-species/1/

Here is what the string looks like:

{
"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."
}


I have tried using the following trimming code:

myUILabel.text = flavorText.trimmingCharacters(in: .whitespacesAndNewlines)


but the line breaks persist. Any suggestions?

Answer

Have you try with replacingOccurrences.

myUILabel.text = flavorText.replacingOccurrences(of target: "\n", with replacement: "")