在iphone中拆分随机字符串
我有一个随机数的字符串 如“12345678912234” 我想把它分成4部分,比如 string1=123 string2=456 string3=7891 string4=2234 有人知道吗 我已尝试使用NSRange,但无法获得它。可能是我用错了在iphone中拆分随机字符串,iphone,objective-c,Iphone,Objective C,我有一个随机数的字符串 如“12345678912234” 我想把它分成4部分,比如 string1=123 string2=456 string3=7891 string4=2234 有人知道吗 我已尝试使用NSRange,但无法获得它。可能是我用错了 我还尝试使用substringToIndex,但也失败了。我认为您需要使用substringWithRange -(NSString *)splitString:(NSString *)string fromX:(int)x toY:(int)
我还尝试使用substringToIndex,但也失败了。我认为您需要使用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]);
刚刚测试过:)工作正常。(只是澄清一下:我对函数的命名不正确,因为它不是toY,而是您希望从X中提取多少字符)。您希望根据哪些标准分割字符串?等子串?随机子串?等等……拆分的标准是什么?总是3,3,4,4分开吗?总是一样长?是的,长度总是一样的。。。