天天看点

Object-c之可变字符串

       //创建可变字符串对象

       NSMutableString *Str1 = [NSMutableString string];

       NSLog(@"Str1 = %@", Str1); // 空字符串

       //在指定位置增加字符串(空字符串只能在0位开始增加)

       [Str1 insertString:@"hello world" atIndex:0];

       NSLog(@"Str1 = %@", Str1);

       [Str1 insertString:@" 中国" atIndex:11];

       NSLog(@"Str1 = %@", Str1);

       // 输出: Str1 = hello world Str1 = hello world 中国

       // 删除指定范围内的字符

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

       NSLog(@"Str1 = %@", Str1);

       // 输出: Str1 = hello 中国

       //追加字符

       [Str1 appendString:@" 哈哈哈"];

       NSLog(@"Str1 = %@", Str1);

       // 输出: Str1 = hello 中国哈哈哈

       //格式化追加字符串

       [Str1 appendFormat:@"%s%d","welcome ", 38];

       NSLog(@"Str1 = %@",Str1);

       // 输出: Str1 = hello 中国哈哈哈welcome 38

       //修改字符串

       [Str1 setString:@"I love you"];

       NSLog(@"Str1 = %@",Str1);

       // 输出: Str1 = I love you

       //替换指定范围内的字符

       [Str1 replaceCharactersInRange:NSMakeRange(2,4) withString: @"fuck哈"];

       NSLog(@"Str1 = %@",Str1);

       // 输出: Str1 = I fuck哈 you

       //用给定的字符串对象替换所有目的的字符串

       [Str1 replaceOccurrencesOfString:@"fuck" withString:@"want fuck" options:NSLiteralSearch range:NSMakeRange(1,8)];

       NSLog(@"Str1 = %@",Str1);

       // 输出: Str1 = I want fuck哈 you

继续阅读