iOS 取出一段字符串里面的数字

+(NSString *)ModifyImidForName:(NSString *)originalStr{
    
   AppDelegate *appdele = (AppDelegate *)[UIApplication sharedApplication].delegate;
    //保存聊天页所有人的昵称,头像,聊天id
    NSMutableDictionary *userInfoDic = [NSMutableDictionary dictionary];
    
    //保存聊天页所有人的聊天id
    NSMutableArray *arr = [NSMutableArray array];
    
    NSString *contentStr;

    //关键代码-------------start
    NSString *regex = @"\\d*";
    NSError *error;
    NSRegularExpression *regular = [NSRegularExpression regularExpressionWithPattern:regex
                                                                             options:NSRegularExpressionCaseInsensitive
                                                                               error:&error];
    // 对str字符串进行匹配
    NSArray *matches = [regular matchesInString:originalStr
                                        options:0
                                          range:NSMakeRange(0, originalStr.length)];
    // 遍历匹配后的每一条记录
    for (NSTextCheckingResult *match in matches) {
        NSRange range = [match range];
        NSString *imid = [originalStr substringWithRange:range];
        
        if(imid.length > 0 && [self isNumber:imid]){
            //            NSLog(@"imid: %@", imid);
            [arr addObject:imid];
        }
    }

    //关键代码-------------end
    
    if(arr.count > 0){
        contentStr = [originalStr mutableCopy];
        for (int i = 0; i < arr.count; i ++) {
            NSString *imid = arr[i];
            
            //如果还没有保存当前的imid,再从数据库获取数据
            if (![[userInfoDic allKeys] containsObject:imid]){
                NSMutableDictionary *dictionary = [appdele getSqliteCurrentData:imid];
                [userInfoDic setObject:dictionary forKey:imid];
            }
            
            //替换id为名字
            contentStr =  [contentStr stringByReplacingOccurrencesOfString:imid withString:userInfoDic[imid][@"name"]];
        }
        //            NSLog(@"mycontentStr: %@",contentStr);
    }
    
    return contentStr;
}

 

转载于:https://my.oschina.net/linxiaoxi1993/blog/716570

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值