//去掉字符串中不要的字符
NSString *str = @"今天天气真好,探索一下NSCharacterSet";
NSString*str1 =@"天气";
NSMutableString *resulStr = [[NSMutableString alloc]init];
for(inti =0; i < str.length; i ++) {
NSString*indexStr = [strsubstringWithRange:NSMakeRange(i,1)];
if(![str1containsString:indexStr]) {
[resulStrappendString:indexStr];
}
}
NSLog(@"常规写法:%@",resulStr);
//利用NSCharacterSet
NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"天气"];
NSArray *setArr = [str componentsSeparatedByCharactersInSet:characterSet];
NSString*resultStr1 = [setArrcomponentsJoinedByString:@""];
NSLog(@"拆分后的字符串数组--%@\n最终字符串--%@",setArr,resultStr1);
网友评论