常见类-NSString
/************** NSString ***************/
//创建了一个空的字符串
NSString *string1=[[NSString alloc]init];
NSString *string2=[NSString string];
NSLog(@"string1:%@",string1);
NSLog(@"string2:%@",string2);
//创建一个字符串常量
NSString *string3=@"Hello World";
NSLog(@"string3:%@ %p",string3,string3);
//根据已有的字符串创建新的字符串(常量区)
NSString *string4_1=[[NSString alloc]initWithString:string3];
NSLog(@"string4:%@ %p",string4_1,string4_1);
NSString *string4_2=[NSString stringWithString:string3];
NSLog(@"string4:%@ %p",string4_2,string4_2);
//根据已有的字符串创建新的字符串(堆区)
NSString *string5_1=[[NSString alloc]initWithFormat:@"%@",string3];
NSLog(@"string5:%@ %p",string5_1,string5_1);
NSString *string5_2=[NSString stringWithFormat:@"%@",string3];
NSLog(@"string5:%@ %p",string5_2,string5_2);
//字符串的比较(指针、内容)
if (string4_1 == string4_2) {//指针的比较
NSLog(@"相同指针");
}
else
NSLog(@"不同指针");
BOOL isSame=[string5_1 isEqualToString:string5_2];//内容的比较
if (isSame) {
NSLog(@"相同内容");
}
else
NSLog(@"不同内容");
//字符串转换成其他类型
NSString *string6_1=@"3.1415926";
float pi=[string6_1 doubleValue];//双精度浮点型
NSLog(@"string6->float:%f",pi);
NSString *string6_2=@"1000.9";
int aInt=[string6_2 intValue];//整型
NSLog(@"string6->integer:%d",aInt);
NSString *string6_3=@"t";
BOOL aBool=[string6_3 boolValue];//布尔类型
NSLog(@"string6->BOOL:%d",aBool);
//求字符串的长度
NSString *string7=@"This-is-a-string";
NSLog(@"%ld",[string7 length]);
//根据已有的字符串 返回新的子字符串(截取其中某些位置的字符串)
NSString *subString1 = [string7 substringFromIndex:13];//从第几位开始截取
NSString *subString2 = [string7 substringToIndex:3];//截取到第几位
NSString *subString3 = [string7 substringWithRange:NSMakeRange(5, 4)];//选择某一区域截取
NSRange range={5,4};
range.location=6;
range.length=3;
NSString *subString4 = [string7 substringWithRange:range];//选择某一区域截取
NSLog(@"subString1:%@",subString1);
NSLog(@"subString2:%@",subString2);
NSLog(@"subString3:%@",subString3);
NSLog(@"subString4:%@",subString4);
//按照字符串中的某个字符转成数组
NSArray *array1=[string7 componentsSeparatedByString:@"-"];
NSLog(@"string->array:%@",array1);
NSString *string8=@"THiS iS a STriNg";
NSLog(@"%@",[string8 uppercaseString]);//大写(不改变string8)
NSLog(@"%@",[string8 lowercaseString]);//小写(不改变string8)
NSLog(@"%@",[string8 capitalizedString]);//首字母大写(不改变string8)
//比较字符串的大小
NSString *string9_1=@"m";
NSString *string9_2=@"a";
NSComparisonResult result = [string9_1 caseInsensitiveCompare:string9_2];
switch (result) {
case NSOrderedAscending:
NSLog(@"string9_1小");
break;
case NSOrderedSame:
NSLog(@"一样大");
break;
case NSOrderedDescending:
NSLog(@"string9_1大");
break;
default:
break;
}
//字符串的查找
NSString *string10=@"abcdefghijklmnopq";
NSRange range1 = [string10 rangeOfString:@"hijk"];
NSLog(@"%@",NSStringFromRange(range1));
NSLog(@"%@",NSStringFromClass([NSString class]));
if (range1.length>0) {
NSLog(@"找到了");
}
else
NSLog(@"没找到");
//判断字符串是不是以某个字符串开头
BOOL isPre=[string10 hasPrefix:@"abc"];
NSLog(@"%d",isPre);
//判断字符串是不是以某个字符串结尾
BOOL isSuf=[string10 hasSuffix:@"opq"];
if (isSuf) {
NSLog(@"string10是以opq结尾的");
}
//字符串的拼接
NSString *string11_1=@"good ";
NSString *string11_2=@"morning";
NSString *string11=[NSString stringWithFormat:@"%@%@",string11_1,string11_2];
NSLog(@"string11:%@",string11);
NSString *string12=[string11_1 stringByAppendingString:string11_2];
NSLog(@"string12:%@",string12);
//子类复用父类成员变量、属性、方法
/************* NSMutableString **************/
//NSMutableString 是 NSString 的子类(继承关系)
NSMutableString *mutableString1=[NSMutableString stringWithFormat:@"Hello_World"];
NSLog(@"mutableString1:%@",mutableString1);
//插入,第一个参数表示需要插入的字符串,第二个参数是放置的位置
[mutableString1 insertString:@"_ios" atIndex:[mutableString1 length]];
NSLog(@"mutableString1:%@",mutableString1);
//替换,第一个参数表示要替换的位置,第二个参数是要替换的字符串
[mutableString1 replaceCharactersInRange:NSMakeRange([mutableString1 length]-3, 3) withString:@"apple"];
NSLog(@"mutableString1:%@",mutableString1);
//删除
NSRange deleteRange=NSMakeRange([mutableString1 length]-6, 6);
[mutableString1 deleteCharactersInRange:deleteRange];
NSLog(@"mutableString1:%@",mutableString1);
NSMutableString *mutableString2=[[NSMutableString alloc]initWithCapacity:5];