#import <Foundation/NSString.h>
#import <Foundation/NSAutoreleasePool.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString *str1 = @"This is a example.";
NSMutableString *mstr = [[NSMutableString alloc] init];
NSRange substr;
//创建可变字符串
mstr = [NSMutableString stringWithString:str1];
NSLog(@"%@",mstr);
//插入字符
[mstr insertString:@"very easy " atIndex:10];
NSLog(@"%@",mstr);
//删除一些字符
[mstr deleteCharactersInRange:NSMakeRange(10,5)];
NSLog(@"%@",mstr);
//查找并删除
substr = [mstr rangeOfString:@"easy "];
if (substr.location != NSNotFound) {
[mstr deleteCharactersInRange:substr];
NSLog(@"%@",mstr);
}
//重新设置字符串
[mstr setString:@"This is string A"];
//替换字符串
[mstr replaceCharactersInRange:NSMakeRange(15, 1) withString:@"BBB"];
NSLog(@"%@",mstr);
//查找第一个并替换
NSString *search = @"This is";
NSString *replace = @"An example of";
substr = [mstr rangeOfString:search];
if (substr.location != NSNotFound) {
[mstr replaceCharactersInRange:substr withString:replace];
NSLog(@"%@",mstr);
}
//查找全部匹配的,并替换
search = @"a";
replace = @"X";
substr = [mstr rangeOfString:search];
while (substr.location != NSNotFound) {
[mstr replaceCharactersInRange:substr withString:replace];
substr = [mstr rangeOfString:search];
}
NSLog(@"%@",mstr);
[pool drain];
return 0;
}