加入收藏 | 设为首页 | 会员中心 | 我要投稿 汽车网 (https://www.0577qiche.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 教程 > 正文

IOS学习笔记十六(NSString和NSMutableString)

发布时间:2023-03-25 11:32:19 所属栏目:教程 来源:
导读:IOS学习笔记十六(NSString和NSMutableString)
1、Nsstring和NSMutableString
Nsstring是不变字符串类,有点像java里面的String,NSMutableString是可变字符串类,有点类似java里面的StringBuffer

2、测试demo
i
IOS学习笔记十六(NSString和NSMutableString)
1、Nsstring和NSMutableString
Nsstring是不变字符串类,有点像java里面的String,NSMutableString是可变字符串类,有点类似java里面的StringBuffer

2、测试demo
int main(int argc,char * argv[]) {
    @autoreleasepool {
        unichar data[6] = {97,98,100,101,102,103};
        Nsstring *str = [[Nsstring alloc] initWithCharacters:data length:6];
        NSLog(@"str is %@",str);
        char *cstr = "chenyu";
        Nsstring *str2 = [Nsstring stringWithUTF8String:cstr];
        NSLog(@"str2 is %@",str2);
        Nsstring *str3 = @"chenyu";
        Nsstring *name = @"陈喻";
        str3 = [str3 stringByAppendingString:@"chenyu"];
        NSLog(@"str3 is %@",str3);
        const char *cstr1 = [str3 UTF8String];
        NSLog(@"cstr1 is %s",cstr1);
        str3 = [str3 stringByAppendingFormat:@"hello %@ hello",name];
        NSLog(@"str3 is %@",str3);
        NSLog(@"str3 length is %lu",[str3 length]);
        Nsstring *s1 = [str3 substringToIndex:10];
        NSLog(@"s1 is %@",s1);
        Nsstring *s2 = [str3 substringFromIndex:5];
        NSLog(@"s2 is %@",s2);
        Nsstring *s3 = [str3 substringWithRange:NSMakeRange(5,10)];
        NSLog(@"s3 is %@",s3);
        NSRange pos = [str3 rangeOfString:@"陈喻"];
        NSLog(@"陈喻在str3中开始的位置:%ld,长度为%ld",pos.location,pos.length);
        str3 = [str3 uppercaseString];
        NSLog(@"str3 is %@",str3);
        NSMutableString *tstr = [NSMutableString stringWithString:@"hello"];
        [tstr appendString:@"chenyu"];
        NSLog(@"tstr is %@",tstr);
        [tstr appendFormat:@"hello word %@",@"chengongyu"];
        NSLog(@"tstr is %@",tstr);
        [tstr insertString:@"hello" atIndex:6];
        NSLog(@"tstr is %@",tstr);
        [tstr deleteCharactersInRange:NSMakeRange(6,9)];
        NSLog(@"tstr is %@",tstr);
        [tstr replaceCharactersInRange:NSMakeRange(3,6) withString:@"objectobject"];
        NSLog(@"tstr is %@",tstr);
    }
}
 
3、运行结果
str is abdefg
str2 is chenyu
str3 is chenyuchenyu
cstr1 is chenyuchenyu
str3 is chenyuchenyuhello 陈喻 hello
str3 length is 26
s1 is chenyuchen
s2 is uchenyuhello 陈喻 hello
s3 is uchenyuhel
陈喻在str3中开始的位置:18,长度为2
str3 is CHENYUCHENYUHELLO 陈喻 HELLO
tstr is hellochenyu
tstr is hellochenyuhello word chengongyu
tstr is hellochellohenyuhello word chengongyu
tstr is hellocuhello word chengongyu
tstr is helobjectobjectllo word chengongyu
 

 

 

 


 

(编辑:汽车网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章