在Cocoa Foundation中的NSString和NSMutableString类,为我们提供了Unicode字符串的支持,NSString和NSMutableString类最大的区别是:NSString--创建赋值以后该字符串的内容与长度不能在动态的更改,除非重新给这个字符串赋值。NSMutableString--创建赋值以后可以动态在该字符串上更改内容与长度。下面是NSString和NSMutableString类的一些常用操作。
1.NSString 创建常量字符串。
NSString *str = @"我是一个字符串 -- This is a String!";
2.NSString 创建空字符,并赋值
NSString *str = [[NSString alloc] init]; str = @"www.babybus.com"; NSLog(@"str内容:%@",str); [astring release]; NSString *str2 = [[NSString alloc] initWithString:@"www.babybus.com"]; NSLog(@"str2:%@",str2); [str2 release]; //用标准c创建字符串:initWithCString方法 char *c_str = "www.babybus.com" NSString *str3 = [[NSString alloc] initWithCString:c_str]; NSLog(@"str3:%@",str3); [str3 release]; //创建格式化字符串:占位符 NSString *str4 = [[NSString alloc] initWithString:[NSString stringWithFormat:@"宝宝巴士拥有%d用户了",70000000]]; NSLog(@"str4:%@",str4); [str4 release]; //从文件初始化字符串 NSString *path = [[NSBundlemainBundle] pathForResource:@"test" ofType:@"text"]; NSString *str = [[NSString alloc] initWithContentsOfFile:path]; NSLog(@"str:%@",str); [str release];
3.NSString 内容写入文件
//获取Documents NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) firstObject];
//路径拼接
NSString *fielPath = [documentsPath stringByAppendingPathComponent:@"str.txt"]; NSString *str = [[NSString alloc] initWithString:@"www.babybus.com"]; NSLog(@"str:%@",str); [str writeToFile:fielPath atomically:YES encoding:NSUTF8StringEncoding error:nil]; [str release];
4.NSString 字符内容比较
NSString *str_1 = @"www.babybus.com"; NSString *str_2 = @"www.babybus.com"; BOOL result = [str_1 isEqualToString:str_2]; if (result) { NSLog(@"内容相同..."); }else { NSLog(@"不好意思,内容不同"); }
//NSOrderedSame判断两者内容是否相同 NSString *str_1 = @"www.babybus.com"; NSString *str_2 = @"www.babybus.com"; BOOL result = [str_1 compare:str_2] == NSOrderedSame; NSLog(@"result:%d",result); //NSOrderedAscending判断两对象值的大小(按字母顺序进行比较,str_2大于str_1为真) NSString *str_1 = @"www.babybus.com"; NSString *str_2 = @"www.babybus.com"; BOOL result = [str_1 compare:str_2] == NSOrderedAscending; NSLog(@"result:%d",result); //NSOrderedDescending判断两对象值的大小(按字母顺序进行比较,str_2小于str_1为真) NSString *str_1 = @"www.babybus.com"; NSString *str_2 = @"www.babybus.com"; BOOL result = [str_1 compare:str_2] == NSOrderedDescending; NSLog(@"result:%d",result);
5.NSString 不考虑大小写内容比较
NSString *str_1 = @"WWW.BABYBUS.COM"; NSString *str_2 = @"www.babybus.com"; //NSCaseInsensitiveSearch:不区分大小写比较 NSLiteralSearch:进行完全比较,区分大小写 NSNumericSearch:比较字符串的字符个数,而不是字符值。 BOOL result = [str_1 compare:str_2 options:NSCaseInsensitiveSearch | NSNumericSearch] == NSOrderedSame; NSLog(@"result:%d",result);
6.NSString 输出大写或者小写字符串
NSString *str_1 = @"www.babybus.com"; NSString *str_2 = @"Good babybus"; NSLog(@"str_1:%@",[str_1 uppercaseString]);//大写 NSLog(@"str_2:%@",[str_2 lowercaseString]);//小写 NSLog(@"str_2:%@",[str_2 capitalizedString]);//首字母大小
7.NSString 查找字符串是否包含其它字符串
NSString *str_1 = @"www.babybus.com"; NSString *str_2 = @"babybus"; NSRange range = [str_1 rangeOfString:str_2]; if (range.location != NSNotFound) { NSLog(@"找到了%@",str_2); }
8.NSString 截取字符串
//substringToIndex: 从字符串的开头一直截取到指定的位置,但不包括该位置的字符 NSString *str_1 = @"www.babybus.com"; NSString *str_2 = [str_1 substringToIndex:3]; NSLog(@"string2--->:%@",str_2); //substringFromIndex: 以指定位置开始(包括指定位置的字符),并包括之后的全部字符 NSString *str_1 = @"www.babybus.com"; NSString *str_2 = [str_1 substringFromIndex:3]; NSLog(@"string2--->:%@",str_2); //substringWithRange: 按照所给出的位置,长度,任意地从字符串中截取子串 NSString *str_1 = @"www.babybus.com"; NSString *str_2 = [str_1 substringWithRange:NSMakeRange(0, 4)]; NSLog(@"string2--->:%@",str_2);
9.NSString 是否以某一字符串开头或者结尾
NSString *str = @"http://wwww.babybus.com"; if([String1 hasPrefix:@"http://"] == 1) { NSLog(@"http://开头") ; } if([String1 hasSuffix:@"com"] == 1) { NSLog(@"com结尾") ; }
10.NSString 获取后缀名
NSString *Path = @"User/SuperDo/string.txt"; NSLog(@"Extension:%@",[Path pathExtension]);
11.NSString 获取拓展路径
NSString *Path = @"User/SuperDo/str.txt"; NSString *absolutePath = [Path stringByExpandingTildeInPath]; NSLog(@"absolutePath:%@",absolutePath); NSLog(@"Path:%@",[absolutePath stringByAbbreviatingWithTildeInPath]);
12. NSMutableString 字符串拼接
//末尾拼接 NSMutableString *str_1 = [[NSMutableString alloc] initWithString:@"www.babybus.com"]; [str_1 appendString:@",good"]; [str_1 appendFormat:@"go --------->"]; NSLog(@"String1:%@",str_1); //在指定位置插入 NSMutableString *str = [[NSMutableString alloc] initWithString:@"www.babybus.com"]; [str insertString:@"Hi! " atIndex:0]; NSLog(@"str:%@",str);
13.NSMutableString 替换字符串
NSMutableString *str = [[NSMutableString alloc] initWithString:@"www.babybus.com is NSMutableString"]; [str replaceCharactersInRange:NSMakeRange(0, 4) withString:@"That"]; NSLog(@"str:%@",str);
本站文章为 宝宝巴士 SD.Team 原创,转载务必在明显处注明:(作者官方网站: 宝宝巴士 )
转载自【宝宝巴士SuperDo团队】 原文链接: http://www.cnblogs.com/superdo/p/4584355.html