美文网首页
NSString+NSMutableString

NSString+NSMutableString

作者: 至于么_ni | 来源:发表于2018-01-05 14:14 被阅读0次

    NSString

    1:创建方式

    (1) NSString * string1 = [NSString alloc] init];        空字符串,没意义

    (2)NSSting * string2 = @"Asher";        将一个字符串直接附值给string2

              NSString * string3 = @"Asher";        string2与string3是同一个对象!!!如果两个对象完全相同,不会创建两个对象

    (3)NSString * string4 = [NSString stringWithFormat:@"Asher"];        格式化

                int a = 10;

                NSString * string5 = [NSString stringWithFormat@"%@ Asher %d",@"Asher1", a];        结果是:Asher1 Asher 10

    2:对字符串的操作

    (1)截取

            NSString * string6 = @"www.Asher.cn";

            NSString * string7 = [string6 substringToIndex:3];        截取头部        结果:www

            NSString * string8 = [string6 substringFromIndex:9];        从第9位开始截        结果:.cn

            NSString * string9 = [string6 substringWithRange:NSMakeRange(4,5)];        截取某段 (eg:从第4为开始截,截取5位        Asher)

    (2)  拼接

            NSString * string10 = @"Ash";

            NSString * string11 = @"er";

            NSString * string12 = [NSString alloc] initWithFormat:@"%@%@",string10,string11];        结果:Asher

            NSString * string13 = [string10 stringByAppendingString:string11];        追加        结果:Asher

            NSString * string14 = [string10 stringByAppendingFormat:@"%@%d",string11,a];        格式化追加        结果:Asher10

    (3)  替换

            NSString * string15 = [string6 stringByReplacingCharactersInRange:NSMakeRange(4,9) withString@"codersWorld.Asher"];        从第4位开始(后面5位),一共9位的内容,替换成codersWorld.Asher        结果:www.codersWorld.Asher.cn

    (4)将文件中的内容初始化字符串

            NSSting * string16 = [NSString stringWithContensOfFile:@"/Users/Asher/Desktop/Asher" encoding:NSUTF8StringEncoding error:nil];

    (5)字符串比较

            [string15 isEqualToString:string16];        比较string15与string16是否相同

    (6)c语言字符串转换成oc字符串

            char cString[100];

            scans(@"%s",cString);

            NSString * string17 = [NSString stringWithCSting:cString encoding:NSASCIIStringEncoding];

    NSMutableString

    1:创建方式

        NSMutableString * string1 = [NSMutableString alloc] init];        空字符串,有意义

        NSMutableString * string2 = [NSMutableString stringWithCapacity:100];        预估值

        NSMutableString * string3 = @"Asher";        string3会退化成不可变字符串

        NSMutableString * string4 = [NSMutableString stringWithstring:@"Asher"];        标准方法

        NSMutableString * string5 = [NSMutableString stringWithFormat:@"Asher"];        标准格式化

        NSMutableString * string6 = [NSMutableString stringWithFormat:@"Asher%d",8];        结果:Asher8

    2:可变字符串的操作

    (1)添加

        [string2 appendString:@"Asher"];        标准

        NSString * string7 = [NSMutableString stringWithstring:@"Asher"];

        [string7 appendFormat:@"8"];        格式化               结果:Asher8

        [string7 appendFormat:@"Asher%d",8]        结果:AsherAsher8

    (2)删除

        NSRange r = [string4 rangeOfString:@"A"];

        [string4 deleteCharactersInRange:r];        把string4中的"A"删除,结果string4 = sher

    (3)替换(修改)

    NSRange r1 = [string4 rangeOfString:@"A"];

    [string4 replaceCharactersInRange:r1 withString@"J"];        把string4中的"A"替换成"J",结果string4 = Jsher

    .

    .

    .

    等用到了别的,再来完善

    相关文章

      网友评论

          本文标题:NSString+NSMutableString

          本文链接:https://www.haomeiwen.com/subject/aewjnxtx.html