OC中的NSString和NSMutableString
//对象方法;NSString * str1 = @"hello,world";//声明OC字符串的对象;
NSString * str2 = [[NSString alloc] initWithString:str1];//用一个字符创建另一个字符串;str也可以写@“其他的字符串”;
NSString * str3 = [[NSString alloc] initWithFormat:@"hello %d %c",5,'A'];//使用格式符,拼接成一个字符串;
//类方法,直接调用
NSString * str4 = [NSString stringWithFormat:@"%d %@ %c",1,@"hello",'R'];
NSString * str5 = [NSString stringWithString:str1];
//以上无论哪种方法创建,都无法改变;
//比较两个字符串是否相等;
BOOL ret = [str1 isEqualToString:str3];
//比较两个字符串的大小;
NSComparisonResult ret1 = [str1 compare:str3];
//返回值为-1,0,1;
//(如果str小于str3,返回-1,等于返回0,大于返回1);
NSLog(@"%ld",ret1);
//从一个字符串中查找一个子字符串;
NSString * str6 = @"I am a so bad man.";
NSString * subStr = @"so";
NSRange range = [str6 rangeOfString:subStr];
NSLog(@"location:%lu length:%lu",range.location,range.length);//location表示位置;length表示长度;
NSString * str10 = @"0123456789";
NSString * str9 = [str10 substringToIndex:6];//从str中提取从头至第6个字符串(不包括第6个),生成新字符串,返回字符串地址,开区间;
NSString * str11 = [str10 substringFromIndex:6];//从str中提取从第6个字符串(包括第6个),到最后一个字符串,生成新字符串,返回字符串地址,闭区间;
NSRange range1 = {3,4};//从第3个字符串开始提取4个字符;
NSString * str7 = [str5 substringWithRange:range1];
//字符串的子类:可变字符串;
//四个方法:增 删 改 查;
//创建;
NSMutableString * nSstr1 = [[NSMutableString alloc]initWithString:@"hello,world!"];
NSMutableString * nSstr2 = [[NSMutableString alloc]init];
[nSstr2 setString:@"创建一个字符串;"];
//增
//追加;append
[nSstr2 appendString:@"123"];
[nSstr2 appendFormat:@"%c",'D'];
//插入 insert
[nSstr1 insertString:@"xxxx" atIndex:4];//在第四个位置中插入四个x
[nSstr1 insertString:[NSString stringWithFormat:@"%d",5] atIndex:5];
//删除 delete
[nSstr1 deleteCharactersInRange:NSMakeRange(3, 5)];//从第三个开始删除5个字符;
//改 替换 replace
[nSstr1 replaceCharactersInRange:NSMakeRange(3, 2) withString:@"qqqqqqqqqqqqqqqqqqqqq"];//从第三个位置开始的2个字符 替换成一大堆q