iOS中的NSString与NSMutableString

2023-11-22 08:40:18 浏览数 (1)

字符串的创建

代码语言:javascript复制
    NSString *str1 = @"ss";//存储在常量区
    NSString *str2 = [NSString stringWithFormat:@"%@",@"ss"];//存储在堆区
    NSString *str3 = [[NSString alloc]initWithFormat:@"%@",@"ss"];//存储在堆区
    NSString *str4 = [NSString stringWithString:@"ss"];//其内部执行的时浅拷贝
    NSLog(@"str1 = %p",str1);
    NSLog(@"str2 = %p",str2);
    NSLog(@"str3 = %p",str3);
    NSLog(@"str4 = %p",str4);

地址打印.png

字符串读写

代码语言:javascript复制
    NSString *str = @"abc";
    NSError *error = nil;
    BOOL flag = [str writeToFile:@"/Users/soso/Desktop/demo1/NSString/demo.txt" atomically:YES encoding:NSUTF8StringEncoding error:&error];
    NSLog(@"%d",flag);

    NSString *str1 = [NSString stringWithContentsOfFile:@"/Users/soso/Desktop/demo1/NSString/demo.txt" encoding:NSUTF8StringEncoding error:&error];
    NSLog(@"%@",str1);//abc

字符串的比较

代码语言:javascript复制
    NSString *str1 = @"ABC";
    NSString *str2 = @"abc";
    
    //比较内容
    NSLog(@"%d",[str1 isEqualToString:str2]);
    //比较地址
    NSLog(@"%d",str1 == str2);
    
    //比较大小,其实是比较的ascii值
    switch ([str1 compare:str2]) {
        case NSOrderedAscending:
            NSLog(@"升序");
            break;
        case NSOrderedSame:
            NSLog(@"相同");
            break;
        case NSOrderedDescending:
            NSLog(@"降序");
            break;
        default:
            break;
    }
    //忽略大小写比较
    switch ([str1 caseInsensitiveCompare:str2]) {
        case NSOrderedAscending:
            NSLog(@"升序");
            break;
        case NSOrderedSame:
            NSLog(@"相同");
            break;
        case NSOrderedDescending:
            NSLog(@"降序");
            break;
        default:
            break;
    }

字符串的搜索

代码语言:javascript复制
    NSString *str = @"https:image.baidu.com.image";
    //判断以什么开头
    if ([str hasPrefix:@"https:"]) {
        NSLog(@"是URL");
    }
    //判断以什么结尾
    if ([str hasSuffix:@"image"]) {
        NSLog(@"是image");
    }
    
    //判断是否包含该字符串
    NSRange range = [str rangeOfString:@"baidu"];
    NSLog(@"位置:%lu--长度:%lu",(unsigned long)range.location,(unsigned long)range.length);//位置:12--长度:5

    
    NSRange range1 = [str rangeOfString:@"baidus"];//没有找到

    if (range1.location == NSNotFound) {
        NSLog(@"没有找到");
    }

字符串截取

代码语言:javascript复制
    NSString *str = @"abcdefgabcd";
    
    NSString *str1 = [str substringWithRange:NSMakeRange(1, 4)];
    NSLog(@"%@",str1);//bcde
    NSLog(@"%@",str);//abcdefgabcd
    
    //截取的起始位置(默认从左往右找,找到就停了)
    NSRange range2 = [str rangeOfString:@"a"];
    NSLog(@"%zd,%zd",range2.location,range2.length);//0,1
    
    //截取的起始位置(可以设置查找方向,找到就停了)
    NSRange range22 = [str rangeOfString:@"a" options:NSBackwardsSearch];
    NSLog(@"%zd,%zd",range22.location,range22.length);//7,1
    
    //从什么地方开始截取,一直到最后
    NSString *str3 = [str substringFromIndex:5];
    NSLog(@"%@",str3);//fgabcd
    NSLog(@"%@",str);//abcdefgabcd
    
    //从开始截取,一直截取到什么位置
    NSString *str4 = [str substringToIndex:5];
    NSLog(@"%@",str4);//fgabcd
    NSLog(@"%@",str);//abcdefgabcd

字符串替换

代码语言:javascript复制
 //替换内容
    NSString *str = @"sayhi!";
    NSString *str2 = [str stringByReplacingOccurrencesOfString:@"hi" withString:@"hello"];
    NSLog(@"%@",str2);//sayhello
    
    //移除两端字符串
    NSString *str4 = @"AAsayAAhi!AA";
    //两端移除字符串集合
    NSString *str3 = [str4 stringByTrimmingCharactersInSet:[NSCharacterSet uppercaseLetterCharacterSet]];
    NSLog(@"%@",str3);

字符串与路径

代码语言:javascript复制
    NSString *pathStr = @"/user/app/Desktop/info.txt";

    //判断是否是绝对路径(本质就是判断字符串是否以/开头)
    if ([pathStr isAbsolutePath]) {
        NSLog(@"绝对路径");
    }
    
    //获取文件路径中的最后一个目录(本质就是获取路径中最后一个目录)
    NSString *lastStr = [pathStr lastPathComponent];
    NSLog(@"%@",lastStr); //info.txt

    //删除路径中的最后一个目录(本质就是删除最后一个斜线的目录,包括斜线)
    NSString *deletStr = [pathStr stringByDeletingLastPathComponent];
    NSLog(@"%@",deletStr);///user/app/Desktop
    
    //给路径添加一个目录(本质就是在字符串末尾加上一个斜线和指定内容)
    NSString *addStr = [pathStr stringByAppendingPathComponent:@"/abc"];
    NSLog(@"%@",addStr);///user/app/Desktop/info.txt/abc
    
    //获取路径中文件的扩展名
    NSString *extensionStr = [pathStr pathExtension];
    NSLog(@"%@",extensionStr);//txt
    
    //删除路径中文件的扩展名(本质就是从字符串的末尾查找,截取第一个后面的内容)
    NSString *deletExtensionStr = [pathStr stringByDeletingPathExtension];
    NSLog(@"%@",deletExtensionStr);///user/app/Desktop/info

    //给文件路径中添加一个扩展名(本质就是在字符串的末尾加上一个点和指定的内容)
    NSString *addExtensionStr = [pathStr stringByAppendingPathExtension:@"jpg"];
    NSLog(@"%@",addExtensionStr);///user/app/Desktop/info.txt.jpg

字符串转换

代码语言:javascript复制
   NSString *str = @"aBcD";
    //转大写
    NSLog(@"%@",[str uppercaseString]);//ABCD

    //转小写
    NSLog(@"%@",[str lowercaseString]);//abcd

    //转数字
    NSString *str1 = @"234";
    int value = [str1 intValue];
    NSLog(@"%d",value);//234

    NSString *str3 = @"abc";
    const char *c = [str3 UTF8String];
    NSLog(@"%s",c);//abc

    const char *c1 = "abc";
    NSString *str4 = [NSString stringWithUTF8String:c1];
    NSLog(@"%@",str4);//abc

NSMutableString

代码语言:javascript复制
 NSMutableString *str = [NSMutableString string];
    NSLog(@"%@",str);
    //字符串拼接
    [str appendString:@"abcdefg"];
    //删除
    [str deleteCharactersInRange:NSMakeRange(2, 2)];
    NSLog(@"%@",str);//abefg
    //插入
    [str insertString:@"PPP" atIndex:4];
    NSLog(@"%@",str);//abefPPPg
    
    //要求字符串找那个的PPP替换AAA(不会改变原来的)
    NSString *str1 = [str stringByReplacingOccurrencesOfString:@"PPP" withString:@"AAA"];
    NSLog(@"%@",str);//abefPPPg
    NSLog(@"%@",str1);//abefAAAg

    //OccurrencesOfString:需要替换的字符串
    //withString:用什么替换
    //options:替换时的搜索方式
    //range:搜索范围
    //返回值:替换了多少个
    NSMutableString *strM = [NSMutableString stringWithFormat:@"abcdefgbc"];
    NSUInteger count = [strM replaceOccurrencesOfString:@"bc" withString:@"KKK" options:0 range:NSMakeRange(0, strM.length)];
    NSLog(@"%@",strM);//aKKKdefgKKK
    NSLog(@"%lu",(unsigned long)count);//2

0 人点赞