NSString 详解
-(void)test{
// 定义常量字符串
NSString *str1 = @"常量字符串";
NSLog(@"str1 : %@",str1);
// 空字符串
NSString *str2 = [[NSString alloc] init];
NSString *str3 = [NSString string];
NSLog(@"str2 : %@",str2);
NSLog(@"str3 : %@",str3);
// 格式化字符串
NSString *str4 = [[NSString alloc] initWithFormat:@"%2f",32.1F];
NSString *str5 = [NSString stringWithFormat:@"%d",29];
NSLog(@"str4 : %@",str4);
NSLog(@"str5 : %@",str5);
// 比较字符串内容和对象
NSString *str6 = @"aaa";
NSString *str7 = [[NSString alloc] initWithFormat:@"aaa"];
if ([str6 isEqualToString:str7]) {
NSLog(@"content str6 equals str7");
}else{
NSLog(@"content str6 is not equals str7");
}
if (str6 == str7) {
NSLog(@"same str6 and str7");
NSLog(@"str6 address : %p str7 address:%p",str6,str7);
}else{
NSLog(@"not same str6 and str7");
NSLog(@"str6 address : %p str7 address:%p",str6,str7);
}
//比较字符串大小(忽略大小写)
NSString *str8 = @"e";
NSString *str9 = @"D";
NSComparisonResult result = [str8 caseInsensitiveCompare:str9];
// 1 为降序 0 为升序
NSLog(@"compare result : %ld",(long)result);
// 转换为基本数据类型
NSString *str10 = @"5.98";
NSLog(@"boolValue : %d",[str10 boolValue]);
NSLog(@"floatValue :%f",[str10 floatValue]);
NSLog(@"doubleValue: %f",[str10 doubleValue]);
NSLog(@"intValue:%d",[str10 intValue]);
// 字符串转换
NSString *str11 = @"hEllo iOs";
NSLog(@"upper:%@",[str11 uppercaseString]);// 大写
NSLog(@"lower:%@",[str11 lowercaseString]);// 小写
NSLog(@"capitalized :%@",[str11 capitalizedString]);// 首字母大写
// 字符串转换成数组
NSString *str12 = @"abc 123 def 456 ghi 789";
NSArray *array = [str12 componentsSeparatedByString:@" "];
NSLog(@"array : %@",array);
// 截取字符串
NSString *str13 = @"0123456789";
// 从第i索引开始截取到末尾
NSLog(@"subStringFromIndex :%@",[str13 substringFromIndex:3]);
// 从索引0开始截取到i索引(不包括i索引)
NSLog(@"subStringToIndex : %@",[str13 substringToIndex:4]);
// 从第i索引开始截取长度为L的字符串
NSRange range = NSMakeRange(2, 3);
// NSRange range = {2,3};
NSLog(@"range: %@",[str13 substringWithRange:range]);
// 字符串拼接
NSString *str14 = @"abc";
NSString *str15 = @"123";
NSLog(@"append string : %@ ",[[str14 stringByAppendingString:str15] stringByAppendingString:str14]);
// 查找字符串
NSString *str16 = @"adfasd367890b2342_af+=234";
NSRange range1 = [str16 rangeOfString:@"67890"];
if (range1.location == NSNotFound) {
NSLog(@"string no found");
}else{
NSLog(@"range1 %@",NSStringFromRange(range1));
}
// 可变字符串
NSMutableString *mutableString1 = [[NSMutableString alloc] initWithString:@"123abc"];
[mutableString1 insertString:@"fff" atIndex:3];
NSLog(@"insertString : %@",mutableString1);
// 替换
[mutableString1 replaceCharactersInRange:NSMakeRange(3, 3) withString:@"xxx"];
NSLog(@"replace : %@",mutableString1);
// 删除
[mutableString1 deleteCharactersInRange:NSMakeRange(3, 3)];
NSLog(@"delete :%@",mutableString1);
}
运行结果如下:
str1 : 常量字符串
str2 :
str3 :
str4 : 32.099998
str5 : 29
content str6 equals str7
not same str6 and str7
str6 address : 0xe30c4 str7 address:0x7b6f24a0
compare result : 1
boolValue : 1
floatValue :5.980000
doubleValue: 5.980000
intValue:5
upper:HELLO IOS
lower:hello ios
capitalized :Hello Ios
array : (
abc,
123,
def,
456,
ghi,
789
)
subStringFromIndex :3456789
subStringToIndex : 0123
range: 234
append string : abc123abc
range1 {7, 5}
insertString : 123fffabc
replace : 123xxxabc
delete :123abc