开发者

splitting random strings in iphone

开发者 https://www.devze.com 2023-03-22 02:54 出处:网络
I have a string with random numbers, like \"12345678912234\" I want to spli开发者_StackOverflowt it into 4 parts, like,

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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号