2

My goal is to slice a string in Swift, for example

var str = "Tower Capital, 99822, Building 2399"

My goal is to slice only Tower Capital, technically what I want is to slice up to the , but not included the comma.

So my goal is to get only "Tower Capital"

1

1 Answer 1

3

You can split the string and get the first part like this:

var str = "Tower Capital, 99822, Building 2399"
let firstItem = str.componentsSeparatedByString(",").first

Or when using Swift >= 3:

str.components(separatedBy: ",").first
Sign up to request clarification or add additional context in comments.

5 Comments

That's an archaic usage. Use str.components(separatedBy: ",").first now...
There might be some people still using Xcode 7... updated my answer :)
@AndréSlotta They really shouldn't be, and we shouldn't be encouraging it :p Swift 3 is already on it's way out lol
@Alexander true :)
Thanks @AndréSlotta

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.