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"

sinusGob
  • 4,053
  • 12
  • 46
  • 82
  • Have a look https://stackoverflow.com/questions/25818197/how-to-split-a-string-in-swift – Sahil Aug 16 '17 at 10:25

1 Answers1

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
André Slotta
  • 13,774
  • 2
  • 22
  • 34