NSString 使用小结

本文详细总结了NSString的常见使用方法和实用技巧,包括字符串拼接、查找替换、格式化输出等核心功能,帮助开发者更好地理解和运用这一Objective-C中的基础类。
摘要由CSDN通过智能技术生成
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        //字符串常量(常量区)
        NSString *string1 = @"字符串常量";
        NSLog(@"string1 : %@", string1);
        
        //空字符串
        NSString *string2 = [[NSString alloc] init];
        NSString *string3 = [NSString string];
        NSLog(@"string2 : %@", string2);
        NSLog(@"string3 : %@", string3);
        
        
        //这两个创建出来的是在常量区
        NSString *string4 = [[NSString alloc] initWithString:@"123"];
        //新版本的SDK已经不推荐使用此方法创建NSString,可以直接常量赋值
        //Using 'stringWithString:'with a literal is redundant
        NSString *string5 = [NSString stringWithString:@"abc"];
        NSLog(@"string4 : %@", string4);
        NSLog(@"string5 : %@", string5);
        
        
        //格式化创建字符串(6.7创建出来的是堆区)
        NSString *string6 = [[NSString alloc] initWithFormat:@"%.2f",31.2F];
        NSString *string7 = [NSString stringWithFormat:@"%d",690];
        NSLog(@"string6 : %@", string6);
        NSLog(@"string7 : %@", string7);
         
        
        //常量区域
        NSString *string8 = [[NSString alloc] initWithString:@"bbb"];
        NSString *string9 = [[NSString alloc] initWithString:@"bbb"];
        //堆区域
        NSString *string10 = [[NSString alloc] initWithFormat:@"bbb"];
        
        //比较内容
        if ([string8 isEqualToString:string9]) {
            NSLog(@"same content");
        }else {
            NSLog(@"different content");
        }
        
        //比较是否为同一个对象
        if (string8 == string9) {
            NSLog(@"same pointer");
        }else {
            NSLog(@"different pointer");
        }
        
        if (string8 == string10) {
            NSLog(@"True!  string8 address: %p    string10 : %p", string8, string10);
        }else {
            NSLog(@"False!  string8 address: %p    string10 : %p", string8, string10);
        }
        
        
        
        
        NSString *string11 = [[NSString alloc] initWithFormat:@"E"];
        NSString *string12 = [[NSString alloc] initWithFormat:@"d"];
        
        //比较字符串大小(忽略大小写)
        NSComparisonResult result = [string11 caseInsensitiveCompare:string12];
        //此时会打印1,表示为降序,如果一样会打印0
        NSLog(@"compare result : %d", result);
        
        
        //转换为基本数据类型
        NSString *string13 = @"5.98";
        NSLog(@"boolValue : %d", [string13 boolValue]);
        NSLog(@"floatValue : %f", [string13 floatValue]);
        NSLog(@"doubleValue : %f", [string13 doubleValue]);
        NSLog(@"intValue : %d", [string13 intValue]);
        
        
        //字符串转换
        NSString *string14 = @"heLlO NsStrinG";
        //转换大写
        NSLog(@"upper : %@", [string14 uppercaseString]);
        //转换小写
        NSLog(@"lower : %@", [string14 lowercaseString]);
        //首个字母大写
        NSLog(@"capitalized : %@", [string14 capitalizedString]);
        
        
        //转换成数组
        NSString *string15 = @"123 abc e21 aa3";
        NSArray *array = [string15 componentsSeparatedByString:@" "];
        NSLog(@"array : %@", array);
        
        //截取字符串
        NSString *string16 = @"123adf";
        //从第2个截取到末尾
        NSLog(@"subStringFromIndex2 : %@", [string16 substringFromIndex:2]);
        //从第开头截取到第4个
        NSLog(@"subStringToIndex4 : %@", [string16 substringToIndex:4]);
        
        //范围截取(从第三个开始截取,截取长度为2个)
        NSRange range = {3, 2};
        NSLog(@"range : %@", [string16 substringWithRange:range]);
        
        
        //字符串拼接
        NSString *appString1 = @"123";
        NSString *appString2 = @"abc";
        NSLog(@"append string : %@", [appString2 stringByAppendingString:appString1]);
    
        
        NSString *rangeString1 = @"adfasd367890b2342_af+=234";
        NSRange range1 = [rangeString1 rangeOfString:@"67890"];
        if(range1.location == NSNotFound) {
            NSLog(@"string not 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:@"bbb"];
        NSLog(@"replace : %@",mutableString1);
        
        //删除
        [mutableString1 deleteCharactersInRange:NSMakeRange(3, 3)];
        NSLog(@"delete : %@",mutableString1);
        
    }
    return 0;
}


日志如下:

string1 : 字符串常量
string2 : 
string3 : 
string4 : 123
string5 : abc
string6 : 31.20
string7 : 690
same content
same pointer
False!  string8 address: 0x30e0    string10 : 0x52b030
compare result : 1
boolValue : 1
floatValue : 5.980000
doubleValue : 5.980000
intValue : 5
upper : HELLO NSSTRING
lower : hello nsstring
capitalized : Hello Nsstring
array : (
    123,
    abc,
    e21,
    aa3
)
subStringFromIndex2 : 3adf
subStringToIndex4 : 123a
range : ad
append string : abc123
range1 {7, 5}
insertString : 123fffabc
replace : 123bbbabc
delete : 123abc



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值