Reputation: 27
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!
Upvotes: 0
Views: 2157
Reputation: 539705
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
Upvotes: 0
Reputation: 93151
You don't need anything fancy, just replace comma with new lines:
let wantedString = exampleString.stringByReplacingOccurrencesOfString(", ", withString: "\n")
Upvotes: 2