iOS Developer

IOS 字符串的创建字符 添加字符 删除字符 替换方法

2016-12-03  本文已影响80人  JakieZhang

//创建方法

NSMutableString*str1 = [[NSMutableStringalloc]init];//空字符串,有意义的

NSMutableString*str2 = [NSMutableStringstringWithCapacity:100];//预估值

NSMutableString*str3 =@"abc";//str3将退化成不可变字符串

NSMutableString*str4 = [NSMutableStringstringWithString:@"string"];//标准创建方法

NSMutableString*str5 = [NSMutableStringstringWithFormat:@"age:%d",6];//格式化创建方法

//添加字符方法

[str2appendString:@"The string"];//标准添加

NSLog(@"%@", str2);

[str2appendString:@" has 6 characters."];

NSLog(@"%@", str2);

NSMutableString*str6 = [NSMutableStringstringWithString:@"string"];

[str6appendFormat:@" has %lu characters", str6.length];//格式化添加

NSLog(@"%@", str6);

//删除字符方法

NSMutableString*str7 = [NSMutableStringstringWithString:@"I am learning Objective-C language."];

//[str7 deleteCharactersInRange:NSMakeRange(14, 10)];

[str7deleteCharactersInRange:[str7rangeOfString:@"Objective-"]];

NSLog(@"%@", str7);

//替换方法

NSMutableString*str8 = [NSMutableStringstringWithString:@"This is a string"];

[str8replaceCharactersInRange:[str8rangeOfString:@"a"]withString:@"another"];

NSLog(@"%@", str8);

上一篇下一篇

猜你喜欢

热点阅读