//字符串的创建 //在可变字符串中 空字符串就有意义 NSMutableString *mString = [[NSMutableString alloc]init]; NSLog(@"mString:%@",mString); //可变字符串不可以与代码区的字符串赋值使用 NSMutableString *mString2 = @"Hello"; //mString2将退化成NSString NSLog(@"mString2:%@",mString2); //可以指定字符串的空间大小 创建字符串 NSMutableString *mString3 =[NSMutableString stringWithCapacity:30]; NSLog(@"mString3:%@",mString3); //可变字符串 添加内容 NSMutableString *mString4 = [[NSMutableString alloc]initWithString:@"Hello"]; [mString4 appendString:@"World"];//给mString4拼接 NSLog(@"mString4:%@",mString4); //可以在指定位置 添加字符串内容 [mString4 insertString:@"123" atIndex:5]; NSLog(@"mString4:%@",mString4); //删除内容 NSMutableString *mString5 = [[NSMutableString alloc]initWithString:@"I am learning Objective-C language."]; //查找字符串内容,在所在字符串中的位置 NSRange range = [mString5 rangeOfString:@"learn"];//需要删除的内容 NSLog(@"range: loc:%lu length:%lu",range.location,range.length); //删除可变字符串中指定的内容 [mString5 deleteCharactersInRange:range]; NSLog(@"mString5:%@",mString5); //替换内容 NSMutableString *mString6 = [[NSMutableString alloc]initWithString:@"HelloWorld!"]; [mString6 replaceCharactersInRange:NSMakeRange(4, 3) withString:@"1234"]; NSLog(@"mString6:%@",mString6);
时间: 2024-10-11 22:41:53