标签:
1 /** 2 * stringByReplacingOccurrencesOfString:字符串替换 3 * componentsSeparateByString:字符串切割 4 * enumerateObjectsUsingBlock打印数组元素 5 */ 6 #import <Foundation/Foundation.h> 7 8 int main(int argc, const char * argv[]) { 9 @autoreleasepool { 10 //字符串替换 11 NSString *str=@"http:**www. bai du.com.ht ml*org"; 12 //方式1:分步替换 13 str=[str stringByReplacingOccurrencesOfString:@" " withString:@""];//先将字符串中的空格替换为空 14 str=[str stringByReplacingOccurrencesOfString:@"*" withString:@"/"];//再将上步的基础上把*替换为/ 15 16 //方式2:一步到位 17 str=[[str stringByReplacingOccurrencesOfString:@" " withString:@""] stringByReplacingOccurrencesOfString:@"*" withString:@"/"];//同上,将两步合并成一步处理 18 19 NSLog(@"%@",str);//打印结果:http://www.baidu.com.html/org 20 21 //使用componentsSeparateByString分割元素 22 NSArray *arry=[@"1,2,3,4,5" componentsSeparatedByString:@","]; 23 //使用enumerateObjectsUsingBlock 打印数组元素 24 [arry enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 25 NSLog(@"%@,%lu",obj,idx) ;//打印结果:1,0 2,1 3,2 4,3 5,4 26 }]; 27 28 //去除首尾空格 29 NSString *str2=@" abcd "; 30 str2=[str2 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; 31 NSLog(@"%@",str2);//打印结果:abcd 32 33 //使用执行字符串进行去除 34 NSString *str3=@"8abcd8"; 35 str3=[str3 stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"8"]]; 36 NSLog(@"%@",str3);//打印结果:abcd 37 38 //去掉首尾小写字母 39 NSString *str4=@"Hello World"; 40 str4=[str4 stringByTrimmingCharactersInSet:[NSCharacterSet lowercaseLetterCharacterSet]]; 41 NSLog(@"%@",str4);//打印结果:Hello W 42 43 NSString *str5=@"hello world"; 44 str5=[str5 stringByTrimmingCharactersInSet:[NSCharacterSet lowercaseLetterCharacterSet]]; 45 NSLog(@"%@",str5);//打印结果: 46 47 //去掉首尾大写字母 48 NSString *str6=@"HelloD"; 49 str6=[str6 stringByTrimmingCharactersInSet:[NSCharacterSet uppercaseLetterCharacterSet]]; 50 NSLog(@"%@",str6);//打印结果:ello 51 52 NSString *str7=@"Hello World"; 53 str7=[str7 stringByTrimmingCharactersInSet:[NSCharacterSet uppercaseLetterCharacterSet]]; 54 NSLog(@"%@",str7);//打印结果:ello World 55 56 //去掉末尾指定字符/字符串 57 NSString *str8=@"Users/apple/desktop/"; 58 str8=[str8 stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"/"]]; 59 NSLog(@"%@",str8);//打印结果:Users/apple/desktop 60 61 } 62 return 0; 63 }
标签:
原文地址:http://www.cnblogs.com/studyrecord/p/4824084.html