针对iOS的学习需要从最基础的开始学起,Foundation框架是OC中最基础也是最重要的框架,他包含了NSString、NSArray、NSSet、NSDictionary、NSData等几个类,我们按照顺序来对这几个类进行讲解。
一、NSString的创建
//创建一个NSString指针,指针指向常量去字符串123
NSString *str1 = @"123";
NSString *str2 = @"123";
//格式化字符串初始化,可以根据自己想要输出字符串的样式来定义字符串
NSString *str3 = [[NSString alloc] initWithFormat:@"123"];
NSString *str4 = [NSString stringWithFormat:@"123"];
NSString *str5 = [NSString stringWithString:@"123"];
NSString *str6 = @"123456";
NSLog(@"str1:%@, address:%p", str1, str1);
NSLog(@"str2:%@, address:%p", str2, str2);
NSLog(@"str3:%@, address:%p", str3, str3);
NSLog(@"str4:%@, address:%p", str4, str4);
NSLog(@"str5:%@, address:%p", str5, str5);
NSLog(@"str6:%@, address:%p", str6, str6);
2018-06-14 16:04:26.773021+0800 NSStringDemo[12750:1058812] str1:123, address:0x101730068
2018-06-14 16:04:26.773198+0800 NSStringDemo[12750:1058812] str2:123, address:0x101730068
2018-06-14 16:04:26.773403+0800 NSStringDemo[12750:1058812] str3:123, address:0xa000000003332313
2018-06-14 16:04:26.773788+0800 NSStringDemo[12750:1058812] str4:123, address:0xa000000003332313
2018-06-14 16:04:26.773879+0800 NSStringDemo[12750:1058812] str5:123, address:0x101730068
2018-06-14 16:04:26.774009+0800 NSStringDemo[12750:1058812] str6:123456, address:0x101730088
根据上面代码显示出:str1、str2、str5的指向同一个内存地址,都是指向了常量区的@“123”,str3、str4指向内存地址相同,并且内存地址的值很高,这说明:他们五个输出的内容虽然相同,但是类型并不同。
二、NSString常用属性
NSString *str1 = @"abcdefg.txt";
NSString *str2 = @"123.0";
NSString *str3 = @"0";
//获取字符串长度
NSLog(@"%ld", str1.length);
//字符串改为大写
NSLog(@"%@", str1.uppercaseString);
//字符串改为小写
NSLog(@"%@", str1.lowercaseString);
//字符串首字母大写
NSLog(@"%@", str1.capitalizedString);
//获取文件后缀
NSLog(@"%@", str1.pathExtension);
//字符串转为NSInteger数字
NSLog(@"%ld", str2.integerValue);
//字符串转为double数字
NSLog(@"%f", str2.doubleValue);
//字符串转为bool值
NSLog(@"%d", str3.boolValue);
//转换为c字符串
NSLog(@"%s", str1.UTF8String);
2018-06-15 11:41:32.221300+0800 NSStringDemo[3874:500667] 11
2018-06-15 11:41:32.221878+0800 NSStringDemo[3874:500667] ABCDEFG.TXT
2018-06-15 11:41:32.223261+0800 NSStringDemo[3874:500667] abcdefg.txt
2018-06-15 11:41:32.223416+0800 NSStringDemo[3874:500667] Abcdefg.Txt
2018-06-15 11:41:32.225640+0800 NSStringDemo[3874:500667] txt
2018-06-15 11:41:32.225917+0800 NSStringDemo[3874:500667] 123
2018-06-15 11:41:32.226166+0800 NSStringDemo[3874:500667] 123.000000
2018-06-15 11:41:32.226307+0800 NSStringDemo[3874:500667] 0
2018-06-15 11:41:32.226431+0800 NSStringDemo[3874:500667] abcdefg.txt
三、NSString常用方法
NSString *str1 = @"abcdefg.txt";
NSString *str2 = @"abclll";
NSString *str3 = @"123\n456\n789";
NSArray *strArr = @[@"111", @"222", @"333"];
//获取某个位置下的字符
char c = [str1 characterAtIndex:0];
NSLog(@"%c", c);
//rangeOfString:获取某个字符串在str1中的range
//NSStringFromRange:把NSRange转换为NSString
NSLog(@"%@", NSStringFromRange([str1 rangeOfString:@"de"]));
//截取字符串
//substringFromIndex:截取得到从某个下标开始到该字符串结束的子字符串
NSLog(@"%@", [str1 substringFromIndex:2]);
//substringToIndex:截取得到从该字符串开始到某个下标为止的子字符串
NSLog(@"%@", [str1 substringToIndex:4]);
//substringWithRange:截取得到range范围内的子字符串
NSLog(@"%@", [str1 substringWithRange:NSMakeRange(3, 2)]);
//componentsSeparatedByString:以某个字符来截取字符串 返回结果是分割子字符串数组
NSLog(@"%@", [str1 componentsSeparatedByString:@"."]);
//字符串比较
//比较返回的结果
//NSOrderedAscending = -1, str1比str2小
//NSOrderedSame, str1和str2相等
//NSOrderedDescending str1比str2大
NSLog(@"%ld", (long)[str1 compare:str2]);
//判断两个字符串是否相等
NSLog(@"%d", [str1 isEqualToString:str2]);
//str1是否由某个字符串开头
NSLog(@"%d", [str1 hasPrefix:@"abc"]);
//str1是否由某个字符串结尾
NSLog(@"%d", [str1 hasSuffix:@"txt"]);
//str1是否包含某个字符串
NSLog(@"%d", [str1 containsString:@"EE"]);
//字符串连接
//str1连接上str2
NSLog(@"%@", [str1 stringByAppendingString:str2]);
//str2连接上上后面的字符串 形成文件目录
NSLog(@"%@", [str2 stringByAppendingPathComponent:@"my.txt"]);
//对数组中的字符串按照插入的字符串进行拼接
NSLog(@"%@", [strArr componentsJoinedByString:@" | "]);
//字符串替换
//用新的字符串替换字符串中某个子字符串
NSLog(@"%@", [str1 stringByReplacingOccurrencesOfString:@"." withString:@"#"]);
//遍历字符串 多行字符串遍历出每一行的内容 stop可以设置循环停止
[str3 enumerateLinesUsingBlock:^(NSString * _Nonnull line, BOOL * _Nonnull stop) {
*stop = YES;
NSLog(@"%@----%d", line, *stop);
}];
2018-06-15 11:43:12.505291+0800 NSStringDemo[3929:506743] a
2018-06-15 11:43:12.505436+0800 NSStringDemo[3929:506743] {3, 2}
2018-06-15 11:43:12.505560+0800 NSStringDemo[3929:506743] cdefg.txt
2018-06-15 11:43:12.505790+0800 NSStringDemo[3929:506743] abcd
2018-06-15 11:43:12.505885+0800 NSStringDemo[3929:506743] de
2018-06-15 11:43:12.506195+0800 NSStringDemo[3929:506743] (
abcdefg,
txt
)
2018-06-15 11:43:12.506369+0800 NSStringDemo[3929:506743] -1
2018-06-15 11:43:12.507340+0800 NSStringDemo[3929:506743] 0
2018-06-15 11:43:12.507598+0800 NSStringDemo[3929:506743] 1
2018-06-15 11:43:12.507916+0800 NSStringDemo[3929:506743] 1
2018-06-15 11:43:12.508174+0800 NSStringDemo[3929:506743] 0
2018-06-15 11:43:12.508366+0800 NSStringDemo[3929:506743] abcdefg.txtabclll
2018-06-15 11:43:12.508693+0800 NSStringDemo[3929:506743] abclll/my.txt
2018-06-15 11:43:12.508879+0800 NSStringDemo[3929:506743] 111 | 222 | 333
2018-06-15 11:43:12.509042+0800 NSStringDemo[3929:506743] abcdefg#txt
2018-06-15 11:43:12.509285+0800 NSStringDemo[3929:506743] 123----1
四、NSMutableString使用
//可变字符串创建
//因为NSMutableString继承于NSString,所以NSString中的方法和属性在NSMutableString中依然适用
NSMutableString *mutableStr1 = [NSMutableString string];
NSMutableString *mutableStr2 = [NSMutableString stringWithFormat:@"1234"];
NSMutableString *mutableStr3 = [[NSMutableString alloc] init];
//可变字符串方法
//拼接字符串
//appendFormat:拼接格式化字符串
[mutableStr1 appendFormat:@"1234"];
NSLog(@"%@", mutableStr1);
//拼接另一个字符串
[mutableStr2 appendString:@"&&&"];
NSLog(@"%@", mutableStr2);
//在某个位置插入一段字符串
[mutableStr1 insertString:@"###" atIndex:1];
NSLog(@"%@", mutableStr1);
//删除某个范围内的字符串
[mutableStr1 deleteCharactersInRange:NSMakeRange(2, 2)];
NSLog(@"%@", mutableStr1);
//字符串替换
[mutableStr1 replaceCharactersInRange:NSMakeRange(2, 2) withString:@"!!"];
NSLog(@"%@", mutableStr1);
2018-06-15 11:45:08.333258+0800 NSStringDemo[3979:513471] 1234
2018-06-15 11:45:08.333610+0800 NSStringDemo[3979:513471] 1234&&&
2018-06-15 11:45:08.333726+0800 NSStringDemo[3979:513471] 1###234
2018-06-15 11:45:08.333838+0800 NSStringDemo[3979:513471] 1#234
2018-06-15 11:45:08.334080+0800 NSStringDemo[3979:513471] 1#!!4