// 01.创建不可变字符串
NSString *str =[NSStringstring]; // 空字符串对象
NSLog(@"str= %@", str);
NSString *str1 =@"此字符串存放于常量区";
NSLog(@"str1= %@",str1);
// 输出: str = (空) str1 =此字符串存放于常量区
//02.格式化创建字符串(类方法)
NSString *str2 =[NSStringstringWithFormat:@"%s%f","hello", 5.2];
NSLog(@"str2= %@", str2);
// 输出: str1 = hello5.200000
//03.利用C语言的字符串生成OC的字符串对象
NSString *str3 =[NSStringstringWithUTF8String:"welloc中国"];
NSLog(@"str3= %@", str3);
// 输出: str3 = welloc中国
//04.将一个字符串复制到另一字符串
NSString *str4 =[NSStringstringWithString:str3];
NSLog(@"str4= %@", str4);
// 输出: str4 = welloc中国
//05.把网址内容生成字符串对象
NSURL *url =[NSURLURLWithString:@"http://fuli.ba"];
NSString *str5 =[NSStringstringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@", str5);
//06.把文件内容生成字符串对象
NSString *str6 =[NSStringstringWithContentsOfFile:@"/Users/fuck/1.html"encoding:NSUTF8StringEncodingerror:nil];
NSLog(@"%@", str6);
/*********************字符串相关操作************************/
NSString *str01= @"hello world";
NSString *str11= @"hello china";
//获取字符串长度
NSLog(@"len= %li",[str01length]);
// 输出: len = 11
//获取指定位置的字符
unichar ch =[str01 characterAtIndex:1];
NSLog(@"ch= %C", ch);// 大写C
// 输出: ch = e
//提取字符串方法(从索引位置提取子串到字符串末尾)
NSString *str02= [str01 substringFromIndex:6];
NSLog(@"str02= %@",str02);// 从0开始
// 输出: str02 = world
//从字符串的开始位置提取子串到索引位置,不包含索引位置的字符
NSString *str03= [str01 substringToIndex:7];
NSLog(@"str03= %@",str03);
// 输出: str03 = hello w
//提取指定范围内的字符串
NSString *str04= [str01 substringWithRange:NSMakeRange(6,3)];
NSLog(@"str04= %@",str04);
// 输出: str04 = wor
//判断两个字符串是否相等
BOOL ret1 =[str01 isEqualToString:str11];
NSLog(@"ret1= %i", ret1);//相等返回yes(1)
// 输出: ret1 = 0
//两个字符串比较
NSComparisonResult ret2 =[str01compare:str11];
NSLog(@"ret2= %li", ret2);
// 输出: ret2 = 1
//追加字符串(不是直接修改原字符串)
NSString *str05= [str01 stringByAppendingString: @"fuck you world ~ ~ ~"];
NSLog(@"str05= %@",str05);
// 输出: str05 = hello world fuck you world ~ ~~u438
//格式化追加字符串(66也没改变)
NSString *str06= [str05 stringByAppendingFormat:@"%c%d", 'u', 438];
NSLog(@"str06= %@", str06);
// 输出: str06 = hello world fuck you world ~ ~ ~
//正序字符串查找
NSRange range1= [str05 rangeOfString:@"world"];
NSLog(@"location1= %li length1 = %li",range1.location,range1.length);
// 输出: location1 = 6 length1 = 5
//倒序字符串查找
NSRange range2= [str05 rangeOfString:@"world" options:NSBackwardsSearch];
NSLog(@"location2= %li length2 = %li",range2.location,range2.length);
// 输出: location2 = 21 length2 = 5
//把数字字符串转化成数字
int a = [@"123"intValue];
NSLog(@"a= %i",a);
float b = [@"00.254"floatValue];
NSLog(@"b= %f", b);
// 输出: a =123 b = 0.254000
//大小写转换
NSLog(@"%@",[@"helloworld"uppercaseString]);
NSLog(@"%@",[@"HELLOWORLD"lowercaseString]);
// 输出: HELLO WORLD hello world
//把每个单词的首字母转换成大写,其余小写
NSLog(@"%@",[@"hElloworlD"capitalizedString]);
// 输出: Hello World
//字符串替换
NSString *str07= [@"hello world hello world "stringByReplacingOccurrencesOfString:@"hello"withString:@"hi"];
NSLog(@"str07= %@", str07);
// 输出: str07 = hi world hi world
//以字符串整体分割
NSString *str = @" #I #am Prime # # #Optimus";
NSArray *dstArray = [str componentsSeparatedByString:@" #"];
NSLog(@"dstArray = %@", dstArray);
NSMutableArray *mulArray = [NSMutableArray arrayWithArray:dstArray];
[mulArray removeObject:@""];
NSLog(@"mulArray = %@", mulArray);
//输出 : dstArray = ("",I,"am Prime","","",Optimus)
//输出 : mulArray = (I,"am Prime ",Optimus)
//以字符集分割字符串
NSArray *dstArray2 = [str componentsSeparatedByCharactersInSet:[NSCharacterSetcharacterSetWithCharactersInString:@"# "] ];
NSLog(@"dstArray2 = %@", dstArray2);
NSMutableArray *mulArray2 = [NSMutableArray arrayWithArray:dstArray2];
[mulArray2 removeObject:@""];
NSLog(@"mulArray2 = %@", mulArray2);
//输出 : dstArray2 =("","",I,"",am,Prime,"","","","","","",Optimus)
//输出 : mulArray2 = (I,am,Prime,Optimus)