// NSString不可变的字符串的类
// 初始化
NSString *str1 = @"字面量";//字面量 (语法糖),⚠️ 不可变
NSString *str2 = [[NSString alloc]initWithFormat:@"initWithFormat: %d",33];
NSLog(@"%@",str1);
NSLog(@"%@",str2);
// 便利构造器
NSString *str3 = [NSString stringWithFormat:@" 便利构造器: %d",23];
NSLog(@"%@",str3);
// 获取字符串长度
// NSString *str4 = @"abcde";
NSString *str4 = [[NSString alloc]initWithFormat:@"蓝鸥科技 公司"];
NSLog(@"%lu", (unsigned long)[str4 length]);
// 判断字符串是否以指定字符串开始或结束
BOOL a = [str4 hasPrefix:@"s"];//前缀
NSLog(@"%d",a);
NSLog(@"%d", [str4 hasSuffix:@"sd"]);//后缀
// 字符串截取
NSLog(@"%@",[str4 substringFromIndex:2]);//从第几个开始截取字符串到结束 包括当前下标
NSLog(@"%@",[str4 substringToIndex:2]);//从0下标开始截取字符串到当前下标 ⚠️不包括当前下标
// 实例化NSRage 变量 然后再执行 substringWithRange
// 获取一个字符串中 字符的rang:
NSRange rang = [str4 rangeOfString:@"蓝鸥"];
NSRange range = NSMakeRange(2, 2);
NSLog(@"%@",[str4 substringWithRange:range]);
// 字符串拼接
NSString *s = [str4 stringByAppendingString:@"3G"];
NSLog(@"%@",s);
NSString *ns = [str4 stringByAppendingFormat:@" %@ ggg",str1];
NSLog(@"%@",ns);
// 字符串比较
NSString *cmp1 = @"ab";
NSString *cmp2 = @"as";
NSLog(@"%ld", [cmp1 compare:cmp2]);
if (NSOrderedAscending == [cmp1 compare:cmp2]) {
NSLog(@"后一个大");
}else if (NSOrderedDescending ==[cmp1 compare:cmp2]){
NSLog(@"前一个大");
}else if (NSOrderedSame ==[cmp1 compare:cmp2]){
NSLog(@"相等");
}
NSLog(@"----%d", [cmp1 isEqualToString:cmp2]);//只能比是否相等 不等返回0 相等返回1
// 字符串替换
// [str4 stringByReplacingCharactersInRange:<#(NSRange)#> withString:<#(NSString *)#>]
NSLog(@"%@", [ns stringByReplacingOccurrencesOfString:@"ggg" withString:@"6666"]);
// 字符串大小写转换
NSString *str5 =@"avcF";
NSLog(@"%@", [str5 uppercaseString]);//全部转换为大写
// NSLog(@"%@", [str5 uppercaseStringWithLocale:<#(NSLocale *)#>])
NSLog(@"%@", [str5 lowercaseString]);//全部转换为小写
NSLog(@"%@", [str5 capitalizedString]);//首字母大写 其余的都小写
// 字符串和数值类型的转换
// 数值类型转换为字符串
NSString *valueStr = [NSString stringWithFormat:@"%d",555];
NSLog(@"字符串类型:%@",valueStr);
// 字符串转换为数值类型
NSLog(@"数值类型:%d", [valueStr intValue]);
// NSMutableString 可变的字符串
NSMutableString *mutableStr1 = [[NSMutableString alloc]initWithFormat:@"avhj"];
NSMutableString *mutableStr2 = [NSMutableString stringWithFormat:@"dsda"];
// 拼接字符串
[mutableStr1 stringByAppendingString:@"dsa"];//使用父类NSString的字符串拼接方法 需要字符串接收
[mutableStr1 appendFormat:@"--s--%@",mutableStr2];//直接拼接到自身
NSLog(@"%@",mutableStr1);
// 插入字符串
NSMutableString *mutableStr3 = [NSMutableString stringWithFormat:@"sdsd"];
[mutableStr3 insertString:@"--www--" atIndex:1];//在 atIndex 这个位置前面查入
NSLog(@"%@",mutableStr3);
// 删除字符串
NSMutableString *mutableStr4 = [NSMutableString stringWithFormat:@"稻草人"];
NSRange range1 = NSMakeRange(0, 2);
[mutableStr4 deleteCharactersInRange:range1];//从开始索引 删除 到 后一个索引之前的
NSLog(@"%@",mutableStr4);
*/
/*
NSString *ns= [NSString stringWithFormat:@"sda.png"];
if ([ns hasSuffix:@"png"]) {
ns = [ns stringByReplacingOccurrencesOfString:@"png" withString:@"jpg"];
} else{
ns = [ns stringByAppendingFormat:@".jpg"];
}
NSLog(@"%@",ns);
NSMutableString *ns1= [NSMutableString stringWithFormat:@"sda.png"];
NSRange rang = [ns1 rangeOfString:@"png"];
if ([ns1 hasSuffix:@"png"]) {
[ns1 replaceCharactersInRange:rang withString:@"jpg"];
} else{
[ns1 stringByAppendingFormat:@".jpg"];
}
NSLog(@"%@",ns1);
*/
// NSArray 常用的方法
/*
不可变数组 :有序 ,下标从 0 开始;
注意:数组存储的只能是对象类型,不能是数值类型
*/
/*
// 初始化方法
NSArray *arr1 = @[@"obj1" ,@"obj2", @"obj3"];//字面量
NSArray *arr2 = [[NSArray alloc]initWithArray:arr1];
NSArray *arr3 = [[NSArray alloc]initWithObjects:@"a",@"s",@"d", nil];
NSArray *arr4 = [NSArray array];//数组初始化 并没有值
NSArray *arr5 = [NSArray arrayWithArray:arr3];
NSArray *arr6 = [NSArray arrayWithObject:@"ds"];
NSArray *arr7 = [NSArray arrayWithObjects:@"22",@"33", nil];
NSLog(@"%@",arr1);
NSLog(@"%@",arr2);
NSLog(@"%@",arr3);
NSLog(@"%@",arr4);
NSLog(@"%@",arr5);
NSLog(@"%@",arr6);
NSLog(@"%@",arr7);
// 获取元素个数
NSLog(@"%lu",[arr1 count]);
// 根据索引值获取对象
NSString *a = [arr1 objectAtIndex:2];
NSLog(@"%@", a);
// 获取对象在数组中的索引值
NSLog(@"%lu", [arr1 indexOfObject:@"obj2"]);
//
// NSMutableArray
// 可变数组,是NSAray的子类,可以增删改等操作
//
//
// 实例化数组对象
NSMutableArray *mutablearr1 = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3", nil];
// 添加元素
[mutablearr1 addObject:@"4"];
[mutablearr1 addObjectsFromArray:@[@"22",@"33"]];
NSLog(@"%@",mutablearr1);
// 插入元素
[mutablearr1 insertObject:@"ss" atIndex:1];
NSLog(@"%@",mutablearr1);
// 删除元素
[mutablearr1 removeObjectAtIndex:1];
[mutablearr1 removeObjectsInArray:@[@"2",@"3"]];
NSLog(@"%@",mutablearr1);
// 替换元素
[mutablearr1 replaceObjectAtIndex:1 withObject:@"aa"];
NSLog(@"%@",mutablearr1);
// 交换指定位置的两个元素
[mutablearr1 exchangeObjectAtIndex:1 withObjectAtIndex:2];
NSLog(@"%@",mutablearr1);
// 数组遍历
for (int i = 0; i <[mutablearr1 count]; i++) {
NSLog(@"%@",mutablearr1[i]);//字面量的形式访问数组元素
NSLog(@"%@",[mutablearr1 objectAtIndex:i]);
}
for (NSString *obj in mutablearr1) {
NSLog(@"%@",obj);
}
*/
/*
NSMutableArray *name = [NSMutableArray arrayWithObjects:@"a",@"b",@"c",@"d", nil];
NSMutableArray *price = [NSMutableArray arrayWithObjects:@"77",@"55",@"32",@"42", nil];
[name addObject:@"q"];
[price addObject:@"92"];
// for (NSString* obj in name) {
// NSLog(@"%@",obj);
// }
// for (NSString* obj in price) {
// NSLog(@"%@",obj);
// }
// for (int i = 0; i < [name count]; i++) {
// NSLog(@"%@---%@",[name objectAtIndex:i],[price objectAtIndex:i]);
// }
[price removeObjectAtIndex:[name indexOfObject:@"c"]];
[name removeObject:@"c"];
// for (int i = 0; i < [name count]; i++) {
// NSLog(@"%@---%@",[name objectAtIndex:i],[price objectAtIndex:i]);
// }
[price replaceObjectAtIndex:[name indexOfObject:@"q"] withObject:@"99"];
for (int i = 0; i < [name count]; i++) {
NSLog(@"%@---%@",[name objectAtIndex:i],[price objectAtIndex:i]);
}
*/
// NSNumber
NSNumber *nm = [NSNumber numberWithInt:3];
NSLog(@"%@",nm);
NSLog(@"%d", [nm intValue]);
NSRange range = NSMakeRange(2, 3);
NSValue *nv = [NSValue valueWithRange:range];
// NSRange r = [nv rangeValue];
NSLog(@"%@",nv);