替换 NSString 中的特定单词 [英] Replace specific words in NSString
问题描述
获取和替换字符串中特定单词的最佳方法是什么?例如我有
NSString * currentString = @"one {two}, thing {thing} good";
现在我需要找到每个 {currentWord}
并为其应用函数
[self replaceWord:currentWord]
然后用函数的结果替换 currentWord
-(NSString*)replaceWord:(NSString*)currentWord;
以下示例展示了如何使用 NSRegularExpression
和 enumerateMatchesInString
来完成任务.我刚刚使用了 uppercaseString
作为替换单词的函数,但您也可以使用 replaceWord
方法:
如果替换的单词是比原词更短或更长(感谢 Fabian Kreiser 注意到这一点!).现在它应该在所有情况下都能正常工作.
NSString *currentString = @"one {two}, thing {thing} good";//正则表达式查找 {...} 包围的单词字符":NSRegularExpression *regex;regex = [NSRegularExpression regularExpressionWithPattern:@"\\{(\\w+)\\}"选项:0错误:NULL];NSMutableString *modifiedString = [currentString mutableCopy];__block int offset = 0;[正则表达式 enumerateMatchesInString:currentString选项:0范围:NSMakeRange(0,[当前字符串长度])usingBlock:^(NSTextCheckingResult *result, NSmatchingFlags 标志, BOOL *stop) {//range = currentString 中正则表达式捕获组(\\w+)"的位置:NSRange range = [result rangeAtIndex:1];//调整已修改字符串的位置:range.location += 偏移量;//获取旧词:NSString *oldWord = [modifiedString substringWithRange:range];//计算新词://在你的情况下,那就是//NSString *newWord = [self replaceWord:oldWord];NSString *newWord = [NSString stringWithFormat:@"--- %@ ---", [oldWord uppercaseString] ];//替换 modifiedString 中的新单词:[修改字符串替换字符输入范围:范围与字符串:新词];//更新偏移量:偏移量 += [新词长度] - [旧词长度];}];NSLog(@"%@", modifiedString);
输出:
<前>一 {--- 二 ---},东西 {--- 东西 ---} 好what is the best way to get and replace specific words in string ? for example I have
NSString * currentString = @"one {two}, thing {thing} good";
now I need find each {currentWord}
and apply function for it
[self replaceWord:currentWord]
then replace currentWord with result from function
-(NSString*)replaceWord:(NSString*)currentWord;
The following example shows how you can use NSRegularExpression
and enumerateMatchesInString
to accomplish the task. I have just used uppercaseString
as function that replaces a word, but you can use your replaceWord
method as well:
EDIT: The first version of my answer did not work correctly if the replaced words are shorter or longer as the original words (thanks to Fabian Kreiser for noting that!) . Now it should work correctly in all cases.
NSString *currentString = @"one {two}, thing {thing} good";
// Regular expression to find "word characters" enclosed by {...}:
NSRegularExpression *regex;
regex = [NSRegularExpression regularExpressionWithPattern:@"\\{(\\w+)\\}"
options:0
error:NULL];
NSMutableString *modifiedString = [currentString mutableCopy];
__block int offset = 0;
[regex enumerateMatchesInString:currentString
options:0
range:NSMakeRange(0, [currentString length])
usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
// range = location of the regex capture group "(\\w+)" in currentString:
NSRange range = [result rangeAtIndex:1];
// Adjust location for modifiedString:
range.location += offset;
// Get old word:
NSString *oldWord = [modifiedString substringWithRange:range];
// Compute new word:
// In your case, that would be
// NSString *newWord = [self replaceWord:oldWord];
NSString *newWord = [NSString stringWithFormat:@"--- %@ ---", [oldWord uppercaseString] ];
// Replace new word in modifiedString:
[modifiedString replaceCharactersInRange:range withString:newWord];
// Update offset:
offset += [newWord length] - [oldWord length];
}
];
NSLog(@"%@", modifiedString);
Output:
one {--- TWO ---}, thing {--- THING ---} good
这篇关于替换 NSString 中的特定单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!