mossy321 mossy321 - 4 years ago 130
iOS Question

How can I trim this Swift string?

I have a bunch of addresses as strings that are in the following example format:

8 Smith st, Sydney, New South Wales, Australia

However, I'd like to trim them down to the following format:

8 Smith st, Sydney

How can I acheive this? Thanks.

Answer Source

here you can trim White space using this

var myString = "    Let's trim the whitespace    "
var newString = myString.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
//Returns "Let's trim the whitespace"

in your Case, first you have to convert it in Array and then convert it as a string as given in below Example

var myString = "Berlin, Paris, New York, San Francisco"
var myArray = myString.componentsSeparatedByString(",")
//Returns an array with the following values:  ["Berlin", " Paris", " New York", " San Francisco"]

For More you can learn From here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download