// 1.去除字符串中的字符串和回车
NSString *string = @" \n I Love China ";
NSLog(@"原始字符串%@",string);
//去掉字符串两端空格
NSString *str1 = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSLog(@"去掉字符串两端空格%@",str1);
//去掉字符串两端空格和回车
NSString *str2 = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet ]];
NSLog(@"去掉字符串两端空格和回车%@",str2);
//去掉字符串中的空格和回车
NSMutableString *mutStr = [NSMutableString stringWithString:string];
NSRange range = {0,string.length};
[mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
NSRange range2 = {0,mutStr.length};
[mutStr replaceOccurrencesOfString:@"\n"withString:@""options:NSLiteralSearch range:range2];
NSLog(@"去掉字符串中的空格和回车%@",mutStr);
// 2.iOS开发之判断数组中是否有重复元素
// 方法一:
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
for (NSNumber *number in arr) {
[dic setObject:number forKey:number];
}
NSLog(@"[dic allValues] %@",[dic allValues]);
// 方法二:
NSArray *arr = @[@1,@2,@1];
NSSet *set = [NSSet setWithArray:arr];
// [set allObjects]得到的是数组
NSArray *newArr = [set allObjects];
NSLog(@"newArr: %@", newArr);
网友评论