+(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;
}