I would like to split an extremely large string, up to 8mb, in 64kb chunks. At the moment I am using the following code:
//1
var regData:String= "string up to 8mb"
var count=((countElements(self.regData!))/65536)
//2
for var index = 0; index < count; ++index {
arr.append(self.regData!.substringWithRange(Range<String.Index>(start: advance(self.regData!.startIndex, 0),end: advance(self.regData!.startIndex, 65536))))
self.regData!.removeRange(Range<String.Index>(start: self.regData!.startIndex, end:advance(self.regData!.startIndex, 65536)))
println(index)
}
//3
println("exit loop")
arr.append(self.regData!)
- I calculate how many 64 kb chunks I have.
- In the for loop I get the first 64kb. I collect them in an array. Now I have to delete the first 64kb strings because of step 3.
- If I I have less than 64kb I get an error in my loop. Therefore my last step is outside the loop.
The code works fine, but it is extremely slow. I need to speed up my code. Do you have any idea how to do it.
Thanks at all.