4、NSString、NSArray
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
// 1.NSString 不可变字符串
/*
// 使用字面量创建不可变字符串对象
NSString *str1 = @"字符串";
// 使用初始化方法创建字符串对象
NSString *str2 = [[NSString alloc] initWithFormat:@"567"];
NSString *str3 = [[NSString alloc] initWithFormat:@"%@", str2];
NSLog(@"%@", str1);
NSLog(@"%@", str2);
NSLog(@"%@", str3);
// 使用便利构造器创建字符串对象
NSString *str4 = [NSString stringWithFormat:@"字符串123 "];
NSLog(@"%@", str4);
// 获取字符串长度
NSUInteger len = [str4 length]; // 给字符串对象发送length消息,可以获取字符串长度
NSLog(@"%lu", len);
// 判断后缀
NSString *str5 = @"icon.png";
BOOL b1 = [str5 hasSuffix:@"png"];
NSLog(@"%d", b1);
// 判断前缀
BOOL b2 = [str5 hasPrefix:@"ic"];
NSLog(@"%d", b2);
// 截取子串
NSString *str6 = [str5 substringWithRange:NSMakeRange(3, 4)];
NSLog(@"%@", str6);
// 拼接字符串
NSString *str7 = @"不是神奇的大炮哥";
NSString *str8 = [str7 stringByAppendingString:@"-续集"];
NSLog(@"%@", str8);
// 替换字符串
NSString *str8_1 = [str7 stringByReplacingOccurrencesOfString:@"神奇" withString:@"奇怪"];
NSLog(@"str8_1 :%@", str8_1);
NSString *str8_2 = [str7 stringByReplacingCharactersInRange:NSMakeRange(2, 2) withString:@"奇怪"];
NSLog(@"str8_2:%@", str8_2);
// 字符串比较
NSString *str9 = @"abc";
NSString *str10 = @"abcd";
NSComparisonResult res = [str9 compare:str10];
NSLog(@"%ld", res);
// 字符串和数值类型转换
NSString *str11 = @"100.9a";
NSLog(@"%.2f", [str11 doubleValue]);
// 大小写转换
NSString *str12 = @"bAo bao";
NSLog(@"str12:%@", [str12 lowercaseString]);
NSLog(@"str12:%@", [str12 uppercaseString]);
NSLog(@"str12:%@", [str12 capitalizedString]);
// 2.可变字符串 能使用不可变字符串的方法,但是不可变字符串不能使用可变字符串的方法
NSMutableString *mutableStu = [[NSMutableString alloc] initWithFormat:@"abdef"];
// 拼接
[mutableStu appendString:@"gh"];
NSLog(@"%@", mutableStu);
// 删除
[mutableStu deleteCharactersInRange:NSMakeRange(2, 2)];
NSLog(@"%@", mutableStu);
// 插入
[mutableStu insertString:@"cccc" atIndex:3];
NSLog(@"%@", mutableStu);
// 修改
[mutableStu replaceCharactersInRange:NSMakeRange(3, 3) withString:@"aaaaa"];
NSLog(@"%@", mutableStu);
*/
// 3.数组
// 初始化
NSArray *namesArray = [[NSArray alloc] initWithObjects:@"a", @"b", nil];
NSLog(@"%@", namesArray);
// 便利构造器
NSArray *namesArray2 = [NSArray arrayWithObjects:@"a", @"b", nil];
// 字面量,不需要加nil
NSArray *namesArray3 = @[@"a a", @"b-b", @"cc", @""];
// 获取元素个数
NSLog(@"%lu", [namesArray3 count]);
// 根据下标获取元素
NSString *str1 = [namesArray3 objectAtIndex:2];
NSString *str2 = namesArray3[2];
// 根据对象获取下标
NSInteger index = [namesArray3 indexOfObject:@"bb"];
NSLog(@"%ld", index);
// NSMutableArray
NSMutableArray *mutableArray = [[NSMutableArray alloc] init];
// 添加
[mutableArray addObjectsFromArray:namesArray3];
NSLog(@"%@", mutableArray);
// 插入
[mutableArray insertObject:@"bei cha ru" atIndex:1];
NSLog(@"%@", mutableArray);
// 删除
[mutableArray removeObjectAtIndex:1];
NSLog(@"%@", mutableArray);
[mutableArray removeLastObject];
// 根据下标交换两个元素
[mutableArray exchangeObjectAtIndex:0 withObjectAtIndex:1];
NSLog(@"%@", mutableArray);
// 替换
[mutableArray replaceObjectAtIndex:2 withObject:@"Z"];
NSLog(@"%@", mutableArray);
}
return 0;
}