且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Objective-C中的字符串替换

更新时间:2023-01-30 17:13:36

你可以使用方法

- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target 
                                        withString:(NSString *)replacement

...获取一个替换了子串的新字符串(参见 NSString 其他人的文件)

...to get a new string with a substring replaced (See NSString documentation for others)

使用示例

NSString *str = @"This is a string";

str = [str stringByReplacingOccurrencesOfString:@"string"
                                     withString:@"duck"];