NSMutableString是oc中的可变字符串和Java中的stringbuilder一样,如果你要经常修改一个字符串的内容,那么最好使用NSMutableString,这样性能更高
void mutbaleString(){ NSMutableString *str=[NSMutableStringstringWithFormat:@"cooljune"]; [strappendString:@"bianbian"];
[str appendFormat:@"bianbian no is %i",10];
NSString *str2=@"sss"; //把str2 copy一份然后再加上添加的字符串变成一个新的对象,str2并为发生改变 NSString *str3=[str2stringByAppendingString:@"kkkkk"]; NSLog(@"拼接之后的字符串是:%@",str); } |
字符串格式化
void stringAppend(){ NSString *qq=@"123456"; NSString *no=@"123456"; NSMutableString *str=[NSMutableStringstringWithFormat:@"http://qq.com?"]; [strappendFormat:@"qq=%@",qq]; [strappendFormat:@"&pwd=%@",no]; NSLog(@"nsstring=%@",str); } |
void stringDelete(){ NSMutableString *s=[NSMutableStringstringWithFormat:@"http://baidu.com/com"];
//NSRange range=[s rangeOfString:@".com"]; //从后往前搜索 NSRange range=[srangeOfString:@"com"options:NSBackwardsSearch]; [s deleteCharactersInRange:range];
NSLog(@"after delete value:%@",s); } |
//字符串替换 void replace(){ NSMutableString *st=[NSMutableStringstringWithFormat:@"baidu.com/app"]; NSRange range=[st rangeOfString:@"app"]; //[st replaceCharactersInRange:range withString:@"apk"]; [stinsertString:@"/web"atIndex:range.location+range.length]; NSLog(@"st---->%@",st); } |