/*________________________NSString(不可变字符串)________________________*/
//________________________1.字符串的创建___________________________________//
//创建字符串常量
NSString *string1 = @"Hello"; //内容不变
string1 = @"Hello world";
//创建字符串
NSString *string2 = [[NSString alloc] initWithString:@"Hello"]; //string2 = hello
NSString *string3 = [[NSString alloc] initWithFormat:@"Hello %@",string2]; //string3 = hello hello
NSLog(@"string2 = %@",string2);
NSLog(@"string3 = %@",string3);
//initWithFormat可以放占位符(格式化符)。有拼接功能(字符串,包括基本数据类型)
//initWtihformat用途示例:
//例如:查询到某一结果,将所有结果拼接后输出
NSString *s1 = @"zhangsan";
NSString *s2 = @"lisi";
NSString *s3 = @"wangwu";
//拼接字符串
NSString *string4 = [NSString stringWithFormat:@"%@,%@,%@",s1,s2,s3];
NSLog(@"%@",string4);
//基本数据类型也可以拼接(initWithFormat)
int age =24;
NSString *string5 = [[NSString alloc] initWithFormat:@"%@的年龄:%d",s1,age];
NSLog(@"string5 = %@",string5);
//________________________2.字符串的比较______________________________________
NSString *string6 = @"abck";
NSString *string7 = @"88888";
//一下比较两个指针地址是否相同,而不是比较字符串中的内容
// if (string6 == string7 )
//* isEqualToString 比较两个字符串中的内容是否相同。(区分大小写)
if ([string6 isEqualToString:string7]) {
NSLog(@"string6与string7内容相同");
}
NSString *string8 = @"abc";
NSString *string9 = @"abc";
if (string8 == string9) {
NSLog(@"string8 = string9"); //YES
}
//对比以下
NSString *string10 = [[NSString alloc] initWithFormat:@"abc%@",@"def"];
NSString *string11 = [[NSString alloc] initWithFormat:@"abc%@",@"def"];
if (string10 == string11) {
NSLog(@"string10 = string11"); //NO
}
if ([string10 isEqualTo: string11]) {
NSLog(@"string10,string11内容相同"); //YES
}
NSString *string12 = [[NSString alloc] initWithString:@"abc"];
NSString *string13 = [[NSString alloc] initWithString:@"abc"];
if (string12 == string13) {
NSLog(@"string12 = string13"); //YES reason:iniWithString不是在堆区上创建的,而是在常量区创建
}
/*
1.initWithString不是在堆区上创建的,而是在常量区创建(此未编译器的优化)
2.iniWithFormat:在堆区创建。
*/
//__________________字符串忽略大小写比较caxeInsensitiveCompare______________
//比较大小
NSString *string14 = @"zhangsan";
NSString *string15 = @"ZHANGSAN";
//caseInsensitiveCompare忽略大小写比较
//typedef NS_ENUM(NSInteger, NSComparisonResult) {NSOrderedAscending = -1L, NSOrderedSame, NSOrderedDescending};
//- (NSComparisonResult)caseInsensitiveCompare:(NSString *)string;
NSComparisonResult result = [string14 caseInsensitiveCompare:string15];
if (result == NSOrderedSame) {
NSLog(@"string14,string15忽略大小写比较相同");
}
NSString *string16 = @"a";
NSString *string17 = @"b";
NSComparisonResult result2 = [string16 compare:string17];
if (result2 == NSOrderedAscending) {
NSLog(@"string16 < string17");
} else if (result2 == NSOrderedSame){
NSLog(@"string16 = string17");
} else if (result2 == NSOrderedSame) {
NSLog(@"string16 > string 17");
}
//_________________3.字符串的其他用法________________
//3.1length:获取字符串的长度
NSString *string18 = @"abcdef"; //@"中文"(长度为2)
NSInteger len = [string18 length];
NSLog(@"leng= %ld",len);
//3.2转大小,写
NSString *string19 = @"hElllo";
//uppercaseString:将字符串中的字母全都转成大写
NSLog(@"upper:%@",[string19 uppercaseString]);
//转小写
NSLog(@"lower:%@",[string19 lowercaseString]);
//首字母大写
NSLog(@"capitalizedString:%@",[string19 capitalizedString]);
//3.3将字符串转成本数据类型
NSString *string20 = @"3.14";
//错误:强转不能改变对象本身
// float f2 = (float) string20; //错误提示:Pointer cannot be cast be cast to type ‘float‘
float f = [string20 floatValue];
NSLog(@"floatValue:%f",f);
NSString *string21 = @"1";
bool b = [string21 boolValue]; //true
NSLog(@"%i",b);
//3.4 字符串的截取(***)
NSString *string22 = @"abcdef";
//substringToIndex:从字符串的开始位置截取到指定位置(不包含指定位置的字符0
NSString *substring1 = [string22 substringToIndex:3];
NSLog(@"subtirng1 = %@",substring1);
//substirngFromIndex:从指定位置截取到字符串的末尾(包含制定位置的字符)
NSString *substring2 = [string22 substringFromIndex:1];
NSLog(@"subtirng2 = %@",substring2);
NSRange rang = {1,4};
// NSRange rang = NSMakeRange(1, 4); //等小于 NSRange rang = {1,4};
NSString *substring3 = [string22 substringWithRange:rang];
NSLog(@"sustirng3 = %@",substring3);
//3.5 字符串的追加
NSString *string23 = @"Android";
//在字符串string23后面追加@" ios"
NSString *string24 = [string23 stringByAppendingString:@" ios"];
NSLog(@"string24 = %@",string24);
NSString *string25 = [string23 stringByAppendingFormat:@" %@%d",@"ios",7];
NSLog(@"string25 = %@",string25);
//3.6 字符串的查找rangeOfString
NSString *string26 = @"www.iphonetrain.com/ios.html";
NSRange rg = [string26 rangeOfString:@"ios"];
//存在没有要查找的字符串问题
//NSNotFound相关定义:enum {NSNotFound = NSIntegerMax};#define NSIntegerMax LONG_MAX
if(rg.length != NSNotFound) {
NSLog(@"location:%ld,length:%ld",rg.location,rg.length);
}