开发者

How to read the character from a string separately in iOS sdk?

I am having string like:

 NSString *str = @"Helloworld.123 456";

I need to get the characters one by one from the string and check whether it is an numeric value.

Is there any predefine开发者_运维知识库d method for this?


To get a character from string you can use -characterAtIndex: method. And NSCharacterSet class allows you to check easily if given character is a number:

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

P.S. If your task is more general then just checking each single character then there also might be a more general built-in method for that task...

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜