lusher00 lusher00 - 19 days ago 4
Swift Question

Swift string tokenization

Why can I do this

let dataArray = data.characters.split{$0 == ";"}.map(String.init)

and this

let dataArray = self.dataIn.characters.split{$0 == "\r\n"}.map(String.init)

but I can't do this?

let dataArray = data.characters.split{$0 == "; "}.map(String.init)

I want to look for the string

"; "


So, you are splitting on characters here so your "$0" is a Character type. Meaning it can never be equal to a non-character object. "; " is a string, since it is not a single character.

That being said, if you are only attempting to split the string by "; ", you should just use the String's components(separatedBy:):

let dataArray = data.components(separatedBy: "; ")