ios - substring with an array string - Swift -
i have array:
var array = ["1|first", "2|second", "3|third"]
how can cut off "1|", "2|", "3|"?
result should this:
println(newarray) //["first", "second", "third"]
you can use (assuming strings contain "|" character):
let newarray = array.map { $0.componentsseparatedbystring("|")[1] }
as @grimxn pointed out, if cannot assume "|" character in strings, use:
let newarray = array.map { $0.componentsseparatedbystring("|").last! }
or
let newarray2 = array.map { $0.substringfromindex(advance(find($0, "|")!, 1)) }
result2 little bit faster, because doesn't create intermediate array componentsseparatedbystring
.
or if want modify original array:
for index in 0..<array.count { array[index] = array[index].substringfromindex(advance(find(array[index], "|")!, 1)) }
Comments
Post a Comment