标签:
NSString *str1 = @"This is a example.";
NSMutableString *mstr = [[NSMutableString alloc] init]; //创建可变字符串
NSRange substr; //子字符串的范围
mstr = [NSMutableString stringWithString:str1];
//插入字符
[mstr insertString:@"very easy " atIndex:10];
//删除一些字符
[mstr deleteCharactersInRange:NSMakeRange(10,5)];
//查找并删除
substr = [mstr rangeOfString:@"easy "];
if (substr.location != NSNotFound) {
[mstr deleteCharactersInRange:substr];
}
//重新设置字符串
[mstr setString:@"This is string A"];
//替换字符串
[mstr replaceCharactersInRange:NSMakeRange(15, 1) withString:@"BBB"];
//查找第一个并替换
NSString *search = @"This is";
NSString *replace = @"An example of";
substr = [mstr rangeOfString:search];
if (substr.location != NSNotFound) {
[mstr replaceCharactersInRange:substr withString:replace];
}
//查找全部匹配的,并替换
search = @"a";
replace = @"X";
substr = [mstr rangeOfString:search];
while (substr.location != NSNotFound) {
[mstr replaceCharactersInRange:substr withString:replace];
substr = [mstr rangeOfString:search];
}
//删除空格
[string stringByReplacingOccurrencesOfString:@" " withString:@""]
标签:
原文地址:http://www.cnblogs.com/huangzs/p/4458815.html