3

I have string like so "/blah//hahaha//lalala/"

Which needs to be translated into an Array of strings so that if I printed the array it would look like this print(arrayOfStrings) // prints ["blah","hahaha","lalala"]

I am new to swift so forgive me if this question is foolish

J. Dutch
  • 85
  • 9
  • Possible duplicate of [Swift: Split a String into an array](http://stackoverflow.com/questions/25678373/swift-split-a-string-into-an-array) – impression7vx Jul 12 '16 at 20:18

2 Answers2

6

Given

let text = "/blah//hahaha//lalala/"

you can

let chunks = String(text.characters.dropFirst().dropLast()).componentsSeparatedByString("//")

and this is what you get

["blah", "hahaha", "lalala"]
Luca Angeletti
  • 58,465
  • 13
  • 121
  • 148
2

There's also the URL solution

let string = "/blah//hahaha//lalala/"
let components = NSURL(fileURLWithPath: string).pathComponents!.dropFirst()
print(components)
vadian
  • 274,689
  • 30
  • 353
  • 361