J. Dutch J. Dutch - 4 months ago 7
Swift Question

How do I get a string between slashes -Swift

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

Answer

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"]
Comments