一.创建字符串(各种方法)
//1.最简单的创建方法
NSString *str1 = @"hello world !";//str1指向常量区
str1 = @"哈哈";//str1(指针)的重指向
NSLog(@"%@",str1);
//2.废柴写法
NSString *str2 = [[NSString alloc]initWithString:@"aa"];
//str2仍然指向常量区
//3.最正宗的写法(指向堆区)
NSString *str3 = [[NSString alloc]initWithFormat:@"%@ %@",str2, str1];
NSString *str4 = [[NSString alloc]initWithFormat:@"%@",@"我是小四"];
NSLog(@"%@",str3);
NSLog(@"%@",str4);
**************2.字符串长度***************
//length计算字符串的长度 返回值是NSUIntger
NSString *str5 = @"dsgtretre";
NSUInteger len = str5.length;//[str5 length]==str5.length
NSLog(@"字符串长度为%lu",len);
**************3.获取字符串前缀、后缀***************
//关键字1:prefix判断前缀 返回值BOOL类型
NSString *str6 = [[NSString alloc]initWithFormat:@"http://www.baidu.com"];
BOOL isPrefix = [str6 hasPrefix:@"http"];
NSLog(@"isPrefix = %d",isPrefix);
BOOL isSuffix = [str6 hasSuffix:@".com"];
NSLog(@"isSuffix = %d",isSuffix);
NSLog(isPrefix && isSuffix ? @"此链接是正确的" : @"此链接不正确");
**************4.查找字符串的范围***************
NSString *str7 = [[NSString alloc] initWithFormat:@"you are beautiful"];
//判断str7中是否含有are这个字符串
//新的数据类型:NSRange结构体类型(两个成员变量);location字符串开始的位置 length字符串的长度
//一旦子串不存在母串中,输出location就会输出一个随机值
NSRange range = [str7 rangeOfString:@"are"]; NSLog(@"location=%ld,length=%ld",range.location,range.length);
NSLog(range.length == 0 ? @"查找失败" : @"查找成功");
**************5.获取子字符串***************
//有三种方法,关键字sub,to,from
NSString *str8 = @"nizhenbang";
//1.给定字符串开始的位置,截取到最后一个字符
//sub--子 super--父
NSString *subStr1 = [str8 substringFromIndex:6];
NSLog(@"subStr1=%@",subStr1);
//2.关键字to,给定结束位置,从第一个字符串开始截取,到指定字符串结束(不包含指定位置上的字符串)
NSString *subStr2 = [str8 substringToIndex:6];
NSLog(@"subStr2=%@",subStr2);
//3.关键字range 任意位置截取任意长度的子串
//先创建range变量,指定要截取额范围
NSRange range1 = [str8 rangeOfString:@"zhen"];
NSString *subStr3 = [str8 substringWithRange:range1];
NSLog(@"subStr3=%@",subStr3);
//第二种方式,不提前设置range变量
NSString *subStr4 = [str8 substringWithRange:NSMakeRange(3, 4)];
//NSMakeRange用来设置NSRange变量(注意非对象类型不要带*)
NSLog(@"subStr4=%@",subStr4);
**************6.字符串拼接***************
//关键字:appending
NSString *str9 = [NSString stringWithFormat:@"HaoRan"];
//在str9后面拼接" is a handsome boy"
NSString *newStr = [str9 stringByAppendingString:@" is a handsome boy"];
NSLog(@"newStr=%@",newStr);
**************7.字符串替换***************
NSString *s1 = [NSString stringWithFormat:@"ihello hello hi hi"];
//1.用指定字符串替换指定内容,比如将hello替换成haha,全部hello都被替换成haha
NSString *newStr1 = [s1 stringByReplacingOccurrencesOfString:@"hello" withString:@"haha"];
NSLog(@"newStr1=%@",newStr1);
//2.指定替换长度,比如将第一个hi改成yes
NSString *newStr2 = [s1 stringByReplacingCharactersInRange:NSMakeRange(16, 2) withString:@" yes"];
NSLog(@"newStr2=%@",newStr2);
**************8.字符串大小写转换***************
NSString *s2 = [NSString stringWithFormat:@"rgreg dxg dgde"];
//1.全部转为大写
NSString *upperStr = [s2 uppercaseString];
NSLog(@"upperStr=%@",upperStr);
//2.全部小写
NSString *lowerStr = [upperStr lowercaseString];
NSLog(@"lowerStr=%@",lowerStr);
//3.首字母转为大写 capital
NSString *captilStr = [lowerStr capitalizedString];
NSLog(@"captilStr=%@",captilStr);
**************9.字符串比较***************
NSString *s3 = @"bcc";
NSString *s4 = @"bbb";
//比较关键字:compare
NSComparisonResult result = [s3 compare:s4];
//NSComparisonResult本质就是NSInteger整型数据.被定义出来只是为了接受对象的比较结果.结果有三种类型 -1(升序,前小后大) 0(值相等) 1(降序,前大后小)
NSLog(@"%ld",result);
网友评论