Objective c 如何在iOS sdk中单独读取字符串中的字符?

Objective c 如何在iOS sdk中单独读取字符串中的字符?,objective-c,Objective C,我有一根弦,像: NSString *str = @"Helloworld.123 456"; 我需要从字符串中逐个获取字符,并检查它是否为数值 有任何预定义的方法吗?要从字符串中获取字符,可以使用-characterAtIndex:方法。和NSCharacterSet类允许您轻松检查给定字符是否为数字: NSCharacterSet* numSet = [NSCharacterSet decimalDigitCharacterSet]; for (int i = 0; i < [st

我有一根弦,像:

 NSString *str = @"Helloworld.123 456";
我需要从字符串中逐个获取字符,并检查它是否为数值


有任何预定义的方法吗?

要从字符串中获取字符,可以使用
-characterAtIndex:
方法。和
NSCharacterSet
类允许您轻松检查给定字符是否为数字:

NSCharacterSet* numSet = [NSCharacterSet decimalDigitCharacterSet];
for (int i = 0; i < [str length];++i)
   if ([numSet characterIsMember: [str characterAtIndex:i]])
   // i-th character is a decimal digit
NSCharacterSet*numSet=[NSCharacterSet decimalDigitCharacterSet];
对于(int i=0;i<[str length];++i)
if([numSet characteristember:[str characterAtIndex:i]]
//第i个字符是十进制数字

另外,如果您的任务更通用,那么只需检查每个字符,那么该任务可能还有一个更通用的内置方法…

要从字符串中获取字符,可以使用
-characterAtIndex:
方法。和
NSCharacterSet
类允许您轻松检查给定字符是否为数字:

NSCharacterSet* numSet = [NSCharacterSet decimalDigitCharacterSet];
for (int i = 0; i < [str length];++i)
   if ([numSet characterIsMember: [str characterAtIndex:i]])
   // i-th character is a decimal digit
NSCharacterSet*numSet=[NSCharacterSet decimalDigitCharacterSet];
对于(int i=0;i<[str length];++i)
if([numSet characteristember:[str characterAtIndex:i]]
//第i个字符是十进制数字
另外,如果你的任务更通用,那么只需检查每个字符,那么可能还有一个更通用的内置任务方法