splitting random strings in iphone
I have a string with random numbers,
like "12345678912234"
I want to spli开发者_StackOverflowt it into 4 parts, like,
string1 = 123
string2 = 456
string3 = 7891
string4 = 2234
anybody has any idea??
I've tried using NSRange but I cannot get it. May be I'm using it wrong.
I also tried using substringToIndex
but it failed too.
I think you need to use substringWithRange.
-(NSString *)splitString:(NSString *)string fromX:(int)x toY:(int)y
{
NSRange range = { x, y};
return [string substringWithRange:range];
}
NSLog([self splitString:@"12345678912234" fromX:0 toY:3]);
NSLog([self splitString:@"12345678912234" fromX:3 toY:3]);
NSLog([self splitString:@"12345678912234" fromX:6 toY:4]);
NSLog([self splitString:@"12345678912234" fromX:10 toY:4]);
Just tested it :) Works fine. (Just to clarify: I have named the function incorrectly as it is not toY but how many characters you want to take in from X).
精彩评论