NSString 详解

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值