首页 > 代码库 > 搜索联系人是去掉拼音中的空格
搜索联系人是去掉拼音中的空格
在搜索联系人时,因为存储进数据库的字段中含有空格,造成在按拼音搜索联系人的时候也必须加入空格,很不方便,所以今天请教大神,在从解析数据的时候就将名字转为拼音,去掉空格,所以在解析时候使用如下代码红色标记的那段:
- (void)syncContact
{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_sync(queue, ^{
NSUserDefaults *pref = [NSUserDefaults standardUserDefaults];
NSString* strToken = [pref stringForKey:@"logintoken"];
//向管理平台登录,获取token
NSString *strUrl = [NSString stringWithFormat:@"%@/syncMember?token=%@",WEB_SERVER_HOST,strToken];
NSURL *url = [NSURL URLWithString:strUrl];
__block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setCompletionBlock:^{
NSString *responseString = [request responseString];
NSError *error = nil;
NSData *jsonData = http://www.mamicode.com/[responseString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];
NSString* msgCode = [dict objectForKey:@"code"];
if ([msgCode isEqualToString:@"200"]) {
NSLog(@"get contact success!");
NSDictionary *memberDict = [dict objectForKey:@"peoples"];
for (NSDictionary* d in memberDict) {
CBContact *contact = [[CBContact alloc]init];
contact.strContactID = [d objectForKey:@"memid"];
contact.strName = [d objectForKey:@"name"];
contact.strTelePhone = [d objectForKey:@"tel"];
contact.strMotion = [d objectForKey:@"motto"];
contact.strAvatar = [d objectForKey:@"avatar"];
if (contact.strAvatar == nil) {
contact.strAvatar = @"";
NSLog(@"-------------- stravatar is null--------------");
}
contact.strEmail = [d objectForKey:@"email"];
contact.strNickName = [d objectForKey:@"nick"];
contact.strBirth = [d objectForKey:@"birth"];
contact.strGender = [d objectForKey:@"gender"];
//公司信息
NSDictionary *corp = [d objectForKey:@"corporation"];
contact.strCorpName = [corp objectForKey:@"corpName"];
contact.strAddress = [corp objectForKey:@"address"];
//去掉中的空格
contact.strPinyin = [[self phonetic:contact.strName]stringByReplacingOccurrencesOfString:@" " withString:@""];
[arrayContacts insertObject:contact atIndex:0];
[self saveContact:contact];
NSLog(@"save contact:%@",contact.strName);
}
//按拼音排序
arrayContacts = [NSMutableArray arrayWithArray:[arrayContacts sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSString *firstPinyin = [(CBContact*)obj1 strPinyin];
NSString *secondPinyin = [(CBContact*)obj2 strPinyin];
return [firstPinyin compare:secondPinyin];
}]];
[self configureSections];
[self.tableContact reloadData];
}
else if([msgCode isEqualToString:@"404"]) {
NSLog(@"找不到用户");
// [SVProgressHUD showErrorWithStatus:@"用户名或密码有误"];
return;
}
else{
;// [SVProgressHUD dismiss];
}
NSLog(@"msgCode is %@",msgCode);
// Use when fetching binary data
//NSData *responseData = http://www.mamicode.com/[request responseData];
dispatch_async(dispatch_get_main_queue(), ^{
[self loadContact];
[self.tableContact reloadData];
});
}];
[request setFailedBlock:^{
NSError *error = [request error];
NSLog(@"login result:%@",error.description);
}];
[request startAsynchronous];
});
}
以上方法时我发送请求并且解析的完整方法,采用了除掉字段中所有空格的方法
附上网上收集的有关空格的方法,供我自己参考:
1.去掉两端的空格
[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]
2.去掉多余的空格
NSString *str = @" this is a test . ";
NSCharacterSet *whitespaces = [NSCharacterSet whitespaceCharacterSet];
NSPredicate *noEmptyStrings = [NSPredicate predicateWithFormat:@"SELF != ‘‘"];
NSArray *parts = [str componentsSeparatedByCharactersInSet:whitespaces];
NSArray *filteredArray = [parts filteredArrayUsingPredicate:noEmptyStrings];
str = [filteredArray componentsJoinedByString:@" "];
3.去掉所有空格
[str stringByReplacingOccurrencesOfString:@" " withString:@""]