我从服务器接收到一个格式如下的字符串:
0_1_2_3
我的任务是从这个字符串中选择数字来填充四个标签。 第一个想法是:
NSString *res1 = [result substringWithRange:NSMakeRange(0, 1)];
[firstLabel setText:res1];
四次带有适当的标签。 但是操作会重复很多次,每次我都会收到一个数字值增加的字符串。因此,当每个数字都是小数时,此代码将不起作用。那么,如何以正确的方式独立于长度跟踪每个数字?
最佳答案
NSString
带有一个名为 -componentsSeparatedByString:
的便捷方法
NSString *myString = @"0_1_2_3";
NSArray *myDigitStrings = [myString componentsSeparatedByString:@"_"];
/* access digit strings from myDigitStrings array by index or fast enumeration... */
for (NSString *myDigitString in myDigitStrings)
NSLog(@"digit string: %@", myDigitString);
关于iphone - 识别 NSString 中的几个整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14225328/