[英]splitting random strings in iphone
I have a string with random numbers,我有一个带有随机数的字符串,
like "12345678912234"像“12345678912234”
I want to split it into 4 parts, like,我想把它分成4个部分,比如,
string1 = 123字符串1 = 123
string2 = 456字符串2 = 456
string3 = 7891字符串3 = 7891
string4 = 2234字符串 4 = 2234
anybody has any idea??有人有任何想法吗?
I've tried using NSRange but I cannot get it.我试过使用 NSRange,但我无法得到它。 May be I'm using it wrong.
可能是我用错了。
I also tried using substringToIndex
but it failed too.我也尝试使用
substringToIndex
但它也失败了。
I think you need to use substringWithRange.我认为您需要使用 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).
(澄清一下:我错误地命名了 function,因为它不是 toY,而是你想从 X 中输入多少个字符)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.