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;
}
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;
}
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- C#中Dictionary类使用实例
- Dictionary扩展基础类向字典中添加键和值
- C#正则表达式Regex类的常用匹配
- C#使用委托(delegate)实现在两个form之间传递数据的方法
- C#利用delegate实现Javascript的each方法
- 详解C#正则表达式Regex常用匹配
- asp dictionary对象的用法
- C#如何遍历Dictionary
- Query中click(),bind(),live(),delegate()的区别
- 详解jquery事件delegate()的使用方法
- jQuery中 delegate使用的问题
- 浅谈jquery中delegate()与live()
- 由点击页面其它地方隐藏div所想到的jQuery的delegate