Object-c 字符串NSString中去掉空格

1,使用NSString中的stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];//方法只是去掉左右两边的空格
2,使用NSString *strUrl = [urlString stringByReplacingOccurrencesOfString:@" " withString:@""];//使用替换的方法去掉空格

示例:

NSString *personIdString = [[NSString alloc] init];
for (id item in _compareDataDic) {
    personIdString = [personIdString stringByAppendingString:[NSString stringWithFormat:@"%@ ",[[_compareDataDic valueForKey:[NSString stringWithFormat:@"%@",item]] valueForKey:@"person_id"]]];
}
personIdString = [personIdString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
personIdString = [personIdString stringByReplacingOccurrencesOfString:@" " withString:@","];


_compareDataDic是一个NSMutableDictionary类型的变量

comments powered by Disqus