Objective-C 中核心处理字符串的类是 NSString 与 NSMutableString ,这两个类最大的区别就是NSString 创建赋值以后该字符串的内容与长度不能在动态的更改,除非重新给这个字符串赋值。而NSMutableString 创建赋值以后可以动态在该字符串上更改内容与长度。
1.创建NSString字符串
NSString 与 char* 最大的区别就是 NSString是一个objective对象,而char* 是一个字节数组。@+" 字符串 " 这个符号为objective-c NSString 字符串常量的标准用法,char* 创建的时候 无需添加@
- -
(void)viewDidLoad - {
-
[super viewDidLoad]; -
-
//经典的字符串赋值 -
NSString *str0 = @"my name is justcoding !"; -
-
//字符串格式化合并分别包括 -
//NSString*类型 int类型 char*类型 -
NSString *str1 = [NSString stringWithFormat:@"我的名字:%@ 我的年龄:%d 我的邮箱:%s",@"justcoding", 25,"justcoding@gmail.com"]; -
-
//字符串赋值 参数中只可以写一个字符串 和第一种很像 -
NSString *str2 = [NSString stringWithString:@"我是字符串"]; -
-
//字符串转换为utf-8格式 参数为char*类型 -
NSString *str3 = [NSString stringWithUTF8String:"字符串转换utf-8格式"]; -
-
-
//字符串合并 -
int i = 100; -
char*c = "xuanyusong"; -
NSString *temp = @"我是临时字符串"; -
-
//在字符串temp的基础继续添加 int i 与 char* c 组成一个新的字符串 -
NSString *str4 = [temp stringByAppendingFormat:@"整型: %d 字符型 :%s",i,c]; -
-
//在字符串temp的基础继续添加temp 并组成一个新的字符串 -
NSString *str5 = [temp stringByAppendingString:temp]; -
-
//字符串输出 -
NSLog(@"str0 = %@", str0); -
NSLog(@"str1 = %@", str1); -
NSLog(@"str2 = %@", str2); -
NSLog(@"str3 = %@", str3); -
NSLog(@"str4 = %@", str4); -
NSLog(@"str5 = %@", str5); -
- }
2.字符串的遍历
每一个字符串其实是由若干个char字符组成,字符串的遍历实际上就是将字符串中的每一个字符提取出来。
- -
(void)viewDidLoad - {
-
[super viewDidLoad]; -
-
//经典的字符串赋值 -
NSString *str = @"YUSONGMOMO"; -
-
//字符串的长度 -
int count = [str length]; -
-
NSLog(@"字符串的长度是%d",count); -
-
//遍历字符串中的每一个字符 -
for(int i =0; i < count; i++) -
{ -
char c = [str characterAtIndex:i]; -
NSLog(@"字符串第 %d 位为 %c",i,c); -
} -
- }
3.字符串的比较
isEqualToString 比较字符串是否完全相等,大小写不一样也无法完全匹配。
hasPrefixe 匹配字符串头
haSuffix
- -
(void)viewDidLoad - {
-
[super viewDidLoad]; -
-
-
NSString *str0 = @"justcoding"; -
NSString *str1 = @"justcoding"; -
-
-
//字符串完全相等比较 -
if([str0 isEqualToString:str1]) -
{ -
NSLog(@"字符串完全相等"); -
} -
-
//字符串以开头比较 -
if([str0 hasPrefix:@"just"]) -
{ -
NSLog(@"字符串str0以just开头"); -
} -
-
//字符串以结尾比较 -
if([str1 hasSuffix:@"coding"]) -
{ -
NSLog(@"str1字符串以coding结尾"); -
} - }
- //isEqualToString方法
- NSString
*astring01 = @"This is a String!"; - NSString
*astring02 = @"This is a String!"; - BOOL
result = [astring01 isEqualToString:astring02]; - NSLog(@"result:%d",result);
- //compare方法(comparer返回的三种值)
- NSString
*astring01 = @"This is a String!"; - NSString
*astring02 = @"This is a String!"; - BOOL
result = [astring01 compare:astring02] = = NSOrderedSame; - NSLog(@"result:%d",result);
- //NSOrderedSame
判断两者内容是否相同
- NSString
*astring01 = @"This is a String!"; - NSString
*astring02 = @"this is a String!"; - BOOL
result = [astring01 compare:astring02] = = NSOrderedAscending; - NSLog(@"result:%d",result);
- //NSOrderedAscending
判断两对象值的大小(按字母顺序进行比较,astring02大于astring01为真)
- NSString
*astring01 = @"this is a String!"; - NSString
*astring02 = @"This is a String!"; - BOOL
result = [astring01 compare:astring02] = = NSOrderedDescending; - NSLog(@"result:%d",result);
- //NSOrderedDescending
判断两对象值的大小(按字母顺序进行比较,astring02小于astring01为真)
- //不考虑大
小写比较字符串1 - NSString
*astring01 = @"this is a String!"; - NSString
*astring02 = @"This is a String!"; - BOOL
result = [astring01 caseInsensitiveCompare:astring02] = = NSOrderedSame; - NSLog(@"result:%d",result);
- //NSOrderedDescending判断两对象值的大小(按字母顺序进行比较,astring02小于astring01为
真)
- //不考虑大小写比较字符串2
- NSString
*astring01 = @"this is a String!"; - NSString
*astring02 = @"This is a String!"; - BOOL
result = [astring01 compare:astring02 options:NSCaseInsensitiveSearch | NSNumericSearch] = = NSOrderedSame; - NSLog(@"result:%d",result);
- //NSCaseInsensitiveSearch:不区分大小写比较
NSLiteralSearch:进行完全比较,区分大小写 NSNumericSearch:比较字符串的字符个数,而不是字符值。
判断的方法可以利用 NSString 类别中 caseInsensitiveCompare: 所回传 -1、0 或是 1 的数值,判定两个字串之间得排序关系,其程式玛如下。
- NSString
*string = @"0"; -
NSComparisonResult result = [string caseInsensitiveCompare:@"A"]; -
-
switch (result) { -
case NSOrderedAscending: -
NSLog(@"升幂"); -
break; -
-
case NSOrderedSame: -
NSLog(@"忽略大小写相同的字串"); -
break; -
-
case NSOrderedDescending: -
NSLog(@"降幂"); -
break; -
-
default: -
NSLog(@"无法判定"); -
break; -
}
4. 文件字符串操作(读写)
- //从文件读取字符串:initWithContentsOfFile方法
- NSString
*path = @"astring.text"; - NSString
*astring = [[NSString alloc] initWithContentsOfFile:path]; - NSLog(@"astring:%@",astring);
- [astring
release]; //astring=nil;
- //写字符串到文件:writeToFile方法
- NSString
*astring = [[NSString alloc] initWithString:@"This is a String!"]; - NSLog(@"astring:%@",astring);
- NSString
*path = @"astring.text"; - [astring
writeToFile: path atomically: YES]; - [astring
release]; //astring=nil;
* ios5 不支持release
- //扩展路径
- NSString
*Path = @"~/NSData.txt"; - NSString
*absolutePath = [Path stringByExpandingTildeIn Path]; - NSLog(@"absolutePath:%@",absolutePath);
- NSLog(@"Path:%@",[absolutePath
stringByAbbreviatingWith TildeInPath]);
5. 字符串的截取和大小写
- //改变字符串的大小写
- NSString
*string1 = @"A String"; - NSString
*string2 = @"String"; - NSLog(@"string1:%@",[string1
uppercaseString]);//大写 - NSLog(@"string2:%@",[string2
lowercaseString]);//小写 - NSLog(@"string2:%@",[string2
capitalizedString]);//首字母大小
- -
(void)viewDidLoad - {
-
[super viewDidLoad]; -
-
-
NSString *str0 = @"中文my name is xuanyusong"; -
-
//截取字符串起始点到index为4之间的内容 -
NSString * to = [str0 substringToIndex:4]; -
-
NSLog(@"to = %@",to); -
-
//截取字符index为2 到结尾之间的内容 -
NSString * from = [str0 substringFromIndex:2]; -
-
NSLog(@"from = %@",from); -
-
-
//设置截取字符串的范围 -
//从第二位开始,长度为十 -
NSRange rang = NSMakeRange(2, 10); -
NSString * strRang = [str0 substringWithRange:rang]; -
NSLog(@"rang = %@",strRang); -
-
//设置字符串首字母大写 -
NSLog(@"str0首字母大写:%@",[str0 capitalizedString]); -
//设置字符串全部内容为大写 -
NSLog(@"str0大写:%@",[str0 uppercaseString]); -
//设置字符全部内容为小写 -
NSLog(@"str0小写:%@",[str0 lowercaseString]); -
- }
(未完待续。。。。。。。。。。。)