Eman Harout
Eman Harout

Reputation: 536

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?

Upvotes: 0

Views: 1323

Answers (1)

Nirav D
Nirav D

Reputation: 72460

Have you try with replacingOccurrences.

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

Upvotes: 2

Related Questions