首页 > 其他 > 详细

Foudation框架之字符串

时间:2016-04-26 10:47:37      阅读:239      评论:0      收藏:0      [点我收藏+]

一 字符串

二 可变字符串的初始化方法

对象方法的初始化

        //init是对象方法

        //将不可变字符串转换为可变字符串

    NSMutableString *str2=[[NSMutableString alloc] initWithString:str1];

        NSLog(@"%ld",str2.length);

//        [str2 insertString:@"lll" atIndex:3];

//        NSLog(@"%@",str2);

        

        //拼接字符串,插入最后前,字符串长度自然多加后面字符串长度 C语言不同长度数组越界 拼接之前 atIndex后面就是 0

        [str2 insertString:@"and lisi" atIndex:str2.length];

        NSLog(@"%@",str2);

        //在后面拼接

        [str2 appendString:@"and lisi"];

        //0开始删除3个字符

        [str2 deleteCharactersInRange:NSMakeRange(6, 5)];

        //替换字符串替换范围内的字符串

        [str2 replaceCharactersInRange:NSMakeRange(6, 5) withString:@"wang"];

        //替换所有的字符的字符串

        [str2 replaceOccurrencesOfString:@"a" withString:@"p" options:NSCaseInsensitiveSearch range:NSMakeRange(0, str2.length)];

        

        

        //C语言字符串转为可变字符串

        char *name="zhangsan";

    NSMutableString *str3=[[NSMutableString alloc] initWithUTF8String:name];

        NSLog(@"%ld",str3.length);

        

        

        //通过格式化来初始化可变字符串

    NSMutableString *str4=[[NSMutableString alloc] initWithFormat:@"hell%d",123];

        NSLog(@"%ld",str4.length);

        

        

        //通过字符串的长度来初始化字符串  0

    NSMutableString *str5=[[NSMutableString alloc] initWithCapacity:10];

        

        

    //直接初始化  0

    NSMutableString *str6=[[NSMutableString alloc] init];

        [str6 insertString:@"lisi" atIndex:0];

        

     //stringwith是类方法

        NSMutableString *str7=[NSMutableString stringWithUTF8String:name];

        NSMutableString *str8=[NSMutableString stringWithFormat:@"hell%d",123];

        NSMutableString *str9=[NSMutableString string];

        NSMutableString *str10=[NSMutableString stringWithString:@"lisi"];

类方法的初始化

Foudation框架之字符串

原文:http://www.cnblogs.com/gzoof/p/5434060.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!