Objective-C 中核心处理字符串的类是 NSString 与 NSMutableString ,
这两个类最大的区别就是NSString 创建赋值以后该字符串的内容与长度不能在动态的更改,除非重新给这个字符串赋值。
而NSMutableString 创建赋值以后可以动态在该字符串上更改内容与长度。
1.创建经典的NSString字符串
1 //经典的字符串赋值 2 NSString *str0=@"i'm aosting"; 3 4 NSString *str1=[NSString stringWithFormat:@"my name:%@ my age %d my email: %s", @"aosting",25, "xueyuan@163.com"]; 5 6 //字符串赋值 参数中只可以写一个字符串 和第一种很像 7 NSString *str2 = [NSString stringWithString:@"我是字符串"]; 8 9 //字符串转换为utf-8格式 参数为char*类型,不用+@ 10 NSString *str3 = [NSString stringWithUTF8String:"字符串转换utf-8格式"]; 11 12 //字符串合并 13 int i = 100; 14 char*c = "xuanyusong"; 15 NSString *temp1 = @"我是临时字符串"; 16 17 //在字符串temp的基础继续添加 int i 与 char* c 组成一个新的字符串 18 NSString *str4 = [temp1 stringByAppendingFormat:@"整型: %d 字符型 :%s",i,c]; 19 20 21 //在字符串temp的基础继续添加temp 并组成一个新的字符串 22 23 NSString *str5 = [temp1 stringByAppendingString:temp1]; 24 25 //字符串输出 26 NSLog(@"str0 = %@", str0); 27 NSLog(@"str1 = %@", str1); 28 NSLog(@"str2 = %@", str2); 29 NSLog(@"str3 = %@", str3); 30 NSLog(@"str4 = %@", str4); 31 NSLog(@"str5 = %@", str5); 32 33
输出:
2013-12-24 14:17:23.734 helloWorld20131021[2141:c07] str0 = i'm aosting
2013-12-24 14:17:23.735 helloWorld20131021[2141:c07] str1 = my name:aosting my age 25 my email: xueyuan@163.com
2013-12-24 14:17:23.735 helloWorld20131021[2141:c07] str2 = 我是字符串
2013-12-24 14:17:23.736 helloWorld20131021[2141:c07] str3 = 字符串转换utf-8格式
2013-12-24 14:17:23.782 helloWorld20131021[2141:c07] str4 = 我是临时字符串整型: 100 字符型:xuanyusong
2013-12-24 14:17:23.783 helloWorld20131021[2141:c07] str5 = 我是临时字符串我是临时字符串
昨天晚上在看c++的时候,就说c++的string和char数组的相同点和区别:
相同点:
不同点:
char数组在拼接的时候容易超出数组的大小。而string就不用这个考虑。
2.字符串的遍历
1 //经典的字符串赋值 2 NSString *str6 = @"YUSONGMOMO"; 3 4 //字符串的长度 5 int count = [str6 length]; 6 7 NSLog(@"字符串的长度是%d",count); 8 9 //遍历字符串中的每一个字符 10 for(int i =0; i < count; i++) 11 { 12 char c = [str6 characterAtIndex:i]; 13 NSLog(@"字符串第 %d 位为 %c",i,c); 14 }
输出:
2013-12-24 14:17:23.783 helloWorld20131021[2141:c07] 字符串的长度是10
2013-12-24 14:17:23.783 helloWorld20131021[2141:c07] 字符串第 0 位为 Y
2013-12-24 14:17:23.784 helloWorld20131021[2141:c07] 字符串第 1 位为 U
2013-12-24 14:17:23.784 helloWorld20131021[2141:c07] 字符串第 2 位为 S
2013-12-24 14:17:23.785 helloWorld20131021[2141:c07] 字符串第 3 位为 O
2013-12-24 14:17:23.785 helloWorld20131021[2141:c07] 字符串第 4 位为 N
2013-12-24 14:17:23.785 helloWorld20131021[2141:c07] 字符串第 5 位为 G
2013-12-24 14:17:23.786 helloWorld20131021[2141:c07] 字符串第 6 位为 M
2013-12-24 14:17:23.786 helloWorld20131021[2141:c07] 字符串第 7 位为 O
2013-12-24 14:17:23.787 helloWorld20131021[2141:c07] 字符串第 8 位为 M
2013-12-24 14:17:23.787 helloWorld20131021[2141:c07] 字符串第 9 位为 O
3.字符串的比较
1 NSString *str7 = @"雨松MOMO"; 2 NSString *str8 = @"雨松MOMO"; 3 4 //字符串完全相等比较 5 if([str7 isEqualToString:str8]) 6 { 7 NSLog(@"字符串完全相等"); 8 } 9 10 //字符串以开头比较 11 if([str7 hasPrefix:@"雨松"]) 12 { 13 NSLog(@"字符串str8以雨松开头"); 14 } 15 16 //字符串以结尾比较 17 if([str8 hasSuffix:@"MOMO"]) 18 { 19 NSLog(@"str8字符串以MOMO结尾"); 20 }
输出:
2013-12-24 14:17:23.787 helloWorld20131021[2141:c07] 字符串完全相等
2013-12-24 14:17:23.788 helloWorld20131021[2141:c07] 字符串str8以雨松开头
2013-12-24 14:17:23.788 helloWorld20131021[2141:c07] str8字符串以MOMO结尾
4.字符串的截取与大小写改变
1 2 NSString *str9 = @"中文my name is xuanyusong"; 3 4 //截取字符串起始点到index为4之间的内容 5 NSString * to = [str9 substringToIndex:4]; 6 7 NSLog(@"to = %@",to); 8 9 //截取字符index为2 到结尾之间的内容 10 NSString * from = [str9 substringFromIndex:2]; 11 12 NSLog(@"from = %@",from); 13 14 //设置截取字符串的范围 15 //从第二位至第十位 16 NSRange rang = NSMakeRange(2, 10); 17 NSString * strRang = [str9 substringWithRange:rang]; 18 NSLog(@"rang = %@",strRang); 19 20 //设置字符串首字母大写 21 NSLog(@"str9首字母大写:%@",[str9 capitalizedString]); 22 //设置字符串全部内容为大写 23 NSLog(@"str9大写:%@",[str9 uppercaseString]); 24 //设置字符全部内容为小写 25 NSLog(@"str9小写:%@",[str9 lowercaseString]); 26 27 NSString *temp = @"is"; 28 NSRange rang1 = [str9 rangeOfString:temp]; 29 30 NSLog(@"搜索的字符串在str9中起始点的index 为 %d", rang1.location); 31 NSLog(@"搜索的字符串在str9中结束点的index 为 %d", rang1.location + rang1.length);
输出:
2013-12-24 14:17:23.788 helloWorld20131021[2141:c07] to = 中文my
2013-12-24 14:17:23.789 helloWorld20131021[2141:c07] from = my name is xuanyusong
2013-12-24 14:17:23.789 helloWorld20131021[2141:c07] rang = my name is
2013-12-24 14:17:23.789 helloWorld20131021[2141:c07] str9首字母大写:中文My Name Is Xuanyusong
2013-12-24 14:17:23.790 helloWorld20131021[2141:c07] str9大写:中文MY NAME IS XUANYUSONG
2013-12-24 14:17:23.790 helloWorld20131021[2141:c07] str9小写:344270255文my name is xuanyusong
5.搜索字符串与替换字符串
1 NSString *temp = @"is"; 2 NSRange rang1 = [str9 rangeOfString:temp]; 3 4 NSLog(@"搜索的字符串在str9中起始点的index 为 %d", rang1.location); 5 NSLog(@"搜索的字符串在str9中结束点的index 为 %d", rang1.location + rang1.length); 6 7 //将搜索中的字符串替换成为一个新的字符串 8 NSString *str = [str9 stringByReplacingCharactersInRange:rang1 withString:@"哇咔咔卡卡咔"]; 9 NSLog(@"替换后字符串为%@", str); 10 11 //将字符串中" " 全部替换成 * 12 str = [str9 stringByReplacingOccurrencesOfString :@" " withString:@"@"]; 13 NSLog(@"替换后字符串为%@", str); 14 15 /* 16 17 传入 NSStringCompareOptions 枚举的值 18 enum{ 19 NSCaseInsensitiveSearch = 1,//不区分大小写比较 20 NSLiteralSearch = 2,//区分大小写比较 21 NSBackwardsSearch = 4,//从字符串末尾开始搜索 22 NSAnchoredSearch = 8,//搜索限制范围的字符串 23 NSNumbericSearch = 64//按照字符串里的数字为依据,算出顺序。例如 Foo2.txt < Foo7.txt < Foo25.txt 24 25 //以下定义高于 mac os 10.5 或者高于 iphone 2.0 可用 26 , 27 NSDiacriticInsensitiveSearch = 128,//忽略 "-" 符号的比较 28 NSWidthInsensitiveSearch = 256,//忽略字符串的长度,比较出结果 29 NSForcedOrderingSearch = 512//忽略不区分大小写比较的选项,并强制返回 NSOrderedAscending 或者 NSOrderedDescending 30 31 //以下定义高于 iphone 3.2 可用 32 , 33 NSRegularExpressionSearch = 1024 34 //只能应用于 rangeOfString:..., stringByReplacingOccurrencesOfString:...和 replaceOccurrencesOfString:... 方法。 35 使用通用兼容的比较方法,如果设置此项,可以去掉 NSCaseInsensitiveSearch 和 NSAnchoredSearch 36 } 37 38 */ 39 40 //设置替换的区域 41 42 NSRange rang2 = NSMakeRange(5, 10); 43 44 //将字符串中" " 全部替换成 * 45 str = [str9 stringByReplacingOccurrencesOfString:@" " withString: @"#" options:1 range:rang2]; 46 NSLog(@"替换后字符串为%@", str);
输出:
2013-12-24 14:41:56.855 helloWorld20131021[2412:c07] 搜索的字符串在str9中起始点的index 为 10
2013-12-24 14:41:56.856 helloWorld20131021[2412:c07] 搜索的字符串在str9中结束点的index 为 12
2013-12-24 14:41:56.856 helloWorld20131021[2412:c07] 替换后字符串为中文my name 哇咔咔卡卡咔 xuanyusong
2013-12-24 14:41:56.857 helloWorld20131021[2412:c07] 替换后字符串为中文my@name@is@xuanyusong
2013-12-24 14:41:56.857 helloWorld20131021[2412:c07] 替换后字符串为中文my name#is#xuanyusong
雨松的博客地址:http://www.xuanyusong.com/archives/381
NSRange:表示范围作用的结构体
有三种方式创建新的 NSRange
1、NSRange range; range.location = 17; range.length = 4;
2、NSRange range = {17 ,4};
3、NSRange range=NSMakeRange(17,4); (推荐)
表示用来处理几何图形的数据类型:NSPoint(点坐标)和 NSSize(长度和宽度)还有一个矩形数据类 型(由点和大小复合而成)NSRect
Cocoa 提供创建这些数据类型方法:NSMakePoint() 、NSMakeSize()和 NAMakeRect()
NSString用法总结:
NSString *heigth =[NSString stringWithFormat: @"Youheigth is %d feet,%d inches", 5,11];
创建的类对象包含了指向超类的指针、 类名和指向类方法的列表的指针。
返回字符串中的字符的个数:
unsigned int length = [heigth length];
返回 Bool 值的字符串比较函数:
- (BOOL) isEqualToString:(NSString *)aString;//比较两个字符串的内容是否相等
还可以使用 compare :方法
1 2 3 // 4 NSString *astring01 = @"This is a String!"; 5 6 NSString *astring02 = @"This is a String!"; 7 8 BOOL result1 = [astring01 compare:astring02] == NSOrderedSame; //NSOrderedSame判断两者内容是否相同 9 10 NSLog(@"result:%d",result1); 11 12 // 13 NSString *astring03 = @"This is a String!"; 14 15 NSString *astring04 = @"this is a String!"; 16 17 BOOL result2 = [astring03 compare:astring04] == NSOrderedAscending; //NSOrderedAscending判断两对象值的大小(按字母顺序进行比较,astring04大于astring03为真) 18 19 NSLog(@"result:%d",result2); 20 21 // 22 NSString *astring05 = @"this is a String!"; 23 24 NSString *astring06 = @"This is a String!"; 25 26 BOOL result3 = [astring05 compare:astring06] == NSOrderedDescending; //NSOrderedDescending判断两对象值的大小(按字母顺序进行比较,astring06小于astring05为真) 27 28 NSLog(@"result:%d",result3); 29 30 NSString *astring07 = @"this is A String!"; 31 32 NSString *astring08 = @"This is a String!"; 33 34 BOOL result4 = [astring07 caseInsensitiveCompare:astring08] == NSOrderedSame; //NSOrderedDescending判断两对象值的大小(按字母顺序进行比较,astring08小于astring07为真) 35 36 NSLog(@"result:%d",result4); 37 38 //2. 39 NSString *astring09 = @"this is a String!"; 40 41 NSString *astring10 = @"This is a String!"; 42 43 BOOL result5 = [astring09 compare:astring10 44 45 options:NSCaseInsensitiveSearch | NSNumericSearch] == NSOrderedSame; //NSCaseInsensitiveSearch:不区分大小写比较 NSLiteralSearch:进行完全比较,区分大小写 NSNumericSearch:比较字符串的字符个数,而不是字符值。 46 47 NSLog(@"result:%d",result5);
输出:
2013-12-24 15:21:00.536 helloWorld20131021[2795:c07] result:1
2013-12-24 15:21:00.537 helloWorld20131021[2795:c07] result:1
2013-12-24 15:21:00.537 helloWorld20131021[2795:c07] result:1
2013-12-24 15:21:00.538 helloWorld20131021[2795:c07] result:1
2013-12-24 15:21:00.538 helloWorld20131021[2795:c07] result:1
-(NSComparisonResult)compare :(NSString *)string;
//逐个字符比较 不区分大小写的比较:
-(NSComparisonResult)compare :(NSString *)string options:(unsigned)mask;
注意:NSComparisonResult 是一个枚举值
options 是一个位掩码,即:
NSCaseInsensitiveSearch:不区分大小写
NSLiteralSearch: 进行完全比较,区分大小写
NSNumericSearch: 比较字符串的字符个数,而不是字符值
检查字符串是否以另一个字符串开头
-(BOOL)hasPrefix : (NSString *)aString;
判断字符串是否是以另一个字符串结尾
-(BOOL)hasSuffix : (NSString *)aString;
如果你想知道字符串内的某处是否包含其他字符串,使用rangeOfString: 方法
1 NSString *string1 = @"This is a string"; 2 3 NSString *string2 = @"string"; 4 5 NSRange range = [string1 rangeOfString:string2]; 6 7 int location = range.location; 8 9 int lenght = range.length; 10 NSLog(@"string2 is in string1 at %d,length: %d",location,lenght);
输出:
2013-12-24 15:25:10.551 helloWorld20131021[2839:c07] string2 is in string1 at 10,length: 6
-(NSRange)rangeOfString :(NSString *)aString;