0

I am receiving a string from a web service. I will set my label text to be the text of this string. However, the string contains a comma, and i want to separate the string to a new line, separated by a comma.

Example:

var exampleString = "street Number,  City"

needs to be

var wantedString = "street Number
                    City"

This is how i currently set my label text:

   self.AdresseLabel.text = self.SpillestedAdresse as String

Any ideas how to split the String?

Thanks!

Goomey
  • 27
  • 7

2 Answers2

2

You don't need anything fancy, just replace comma with new lines:

let wantedString = exampleString.stringByReplacingOccurrencesOfString(", ", withString: "\n")
Code Different
  • 90,614
  • 16
  • 144
  • 163
0

If your intention is to replace a comma (followed by zero, one or more space characters) by a newline character, then a string replacement with a regular expression pattern is an option:

let exampleString = "street Number,  City"
let wantedString = exampleString.stringByReplacingOccurrencesOfString(",\\s*",
    withString: "\n",
    options: .RegularExpressionSearch)

print(wantedString)

Output:

street Number
City
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382