I have an array:
var array = ["1|First", "2|Second", "3|Third"]
How I can cut off "1|", "2|", "3|"?
The result should look like this:
println(newarray) //["First", "Second", "Third"]
You can use (assuming the strings will contain the "|" character):
let newarray = array.map { $0.componentsSeparatedByString("|")[1] }
As @Grimxn pointed out, if you cannot assume that the "|" character will always be in the strings, use:
let newarray = array.map { $0.componentsSeparatedByString("|").last! }
or
let newarray2 = array.map { $0.substringFromIndex(advance(find($0, "|")!, 1)) }
result2 could be a little bit faster, because it doesn't create an intermediate array from componentsSeparatedByString
.
or if you want to modify the original array:
for index in 0..<array.count {
array[index] = array[index].substringFromIndex(advance(find(array[index], "|")!, 1))
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With