NSString里面的数字

0-9ASCII码:48 - 57

 

方法一:

//遍历NSString里面的数字

    NSString *str = @"ssdg0db9f567dazy";

    NSMutableString *strM = [NSMutableString string];

    for (int i = 0; i < str.length; i++) {

        unichar a =[str characterAtIndex:i];        

        if (!(a > 47 && a < 58) ) {

            //拼接字符串:方法一

            NSString *strResult = [str substringWithRange:NSMakeRange(i, 1)];

            [strM appendString:strResult];

            //拼接字符串:方法二

            //[strM appendFormat:@"%c",a];

        }

    }

  NSLog(@“%@",strM);

 

方法二:利用正则表达式来实现

#import "GJViewController.h"

#import "NSString+Regex.h"

@interface GJViewController ()

@end

@implementation GJViewController

- (void)viewDidLoad

{

    [super viewDidLoad];

NSString *str = @"zhang23g114jn889"; 

    NSRegularExpression *regex = [NSRegularExpression

                                  regularExpressionWithPattern:@"\\D*"

                                  options:NSRegularExpressionCaseInsensitive | NSRegularExpressionDotMatchesLineSeparators

                                  error:nil];

    NSArray *result = [regex matchesInString:str options:0 range:NSMakeRange(0, str.length)];

    NSMutableString *strM = [NSMutableString string];

    for (NSTextCheckingResult *resultCheck in result) {

        if (resultCheck.range.length > 0) {

            [strM appendString:[str substringWithRange:resultCheck.range]];

        }

    }

    NSLog(@"%@",strM);

}

 

posted @ 2015-07-08 10:29  树籽  阅读(271)  评论(0编辑  收藏  举报