2015-06-28 85 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

即使這是我正在做的事,我也會接受這個答案,因爲我正在做的是正確的。錯誤是我使用「|||」而不是來自字符串的字符,顯然他們沒有相同的字符(儘管它們應該是) –