특정 문자에 대한 분할(componentsSeparatedByString)

2016. 8. 3. 12:46Programming/Swift

반응형
let str = "man|17&woman|19"

let result = str.componentsSeparatedByString("&")

print(result)  // ["man|17", "woman|19]



let charSet = NSCharacterSet(charactersInString: "|^")

let result = str.componentsSeparatedByCharactersInSet(charSet)

print(result)  // ["man","17","woman","19"]



let Array2D = str.componentsSeparatedByString("^").map {

    $0.componentsSeparatedByString("|")

}


print(Array2D) // [["man,"17"],["woman","19"]]








반응형