2015-06-28 83 views
0

我有一个很长的字符串,我需要通过分割分割成一个数组,当“|||”被发现分裂NSString不能在Swift中工作

我可以拆分使用两种方式,我发现在SO

第一个的字符串是本

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) 
} 

第二个是该

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

我使用定界符作为试图“[\ x7C] [\ x7C] [\ x7C]”和“|||”我试图用字符串和NSString的

似乎没有任何工作,虽然,我只是得到与它原始字符串数组

+2

VAR SPLT = str.componentsSeparatedByString(“|||”)应工作,军队是现场 –

+0

你想做什么?这个字符串从哪里来? –

+2

'var splits =“aosd ||| aoisnd ||| aipsnd”.componentsSeparatedByString(“|||”)'** does ** work。 – luk2302

回答

1
func split(splitter: String) -> [String] { 
    return splitter.componentsSeparatedByString("|||") 
} 
+0

即使这是我正在做的事,我也会接受这个答案,因为我正在做的是正确的。错误是我使用“|||”而不是来自字符串的字符,显然他们没有相同的字符(尽管它们应该是) –