您的位置:首页 > 移动开发 > IOS开发

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

2016-07-23 10:55 477 查看
+(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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息