#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc]init];
NSString *dog=@"Hotdog? I thought you said hotfrog!";
NSMutableString *mute;
mute = [NSMutableString stringWithString:dog];
NSLog(@"%@", mute);
[mute setString:@"I am a new string "];
NSLog(@"%@", mute);
[mute replaceCharactersInRange: NSMakeRange(11, 12) withString: @"mother"];
NSLog(@"%@", mute);
[pool drain];
return 0;
}
答案 0 :(得分:-1)
您想使用方法
- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target
withString:(NSString *)replacement
因此target
为@"string"
而replacement
为@"Mother"
。所以你的程序看起来像这样:
NSString *originalString = @"I am a new string ";
NSLog(@"Here is the original string: %@", originalString);
NSString *newString = [originalString stringByReplacingOccurrencesOfString:@"string" withString:@"Mother"];
NSLog(@"And here is the new string: %@", newString);
还列出了其他类似方法here in the doco。
注意,如果您明确要使用NSMutableString
,请在您的问题中说明。