dimanche 28 juin 2015

Splitting NSString doesn't work in Swift

I have a long string i need split into an array by splitting when "|||" is found

I can split a String using two ways i found at SO

First one is this

 func split(splitter: String) -> Array<String> {
    let regEx = NSRegularExpression(pattern: splitter, options: NSRegularExpressionOptions(), error: nil)!
    let stop = "<SomeStringThatYouDoNotExpectToOccurInSelf>"
    let modifiedString = regEx.stringByReplacingMatchesInString (self, options: NSMatchingOptions(),
        range: NSMakeRange(0, count(self)),
        withTemplate:stop)
    return modifiedString.componentsSeparatedByString(stop)
}

Second one is this

var splt = str.componentsSeparatedByString("[\\x7C][\\x7C][\\x7C]")

I tried using the delimiter as both "[\x7C][\x7C][\x7C]" and "|||" and i tried using both String and NSString

Nothing seems to work though, i just get an array with the original string in it

Aucun commentaire:

Enregistrer un commentaire