您应该将返回值分配给 a NSString
:
NSString *replaced = [chars stringByReplacingOccurrencesOfString:[self convertDecimalToChar:num] withString:[self convertDecimalToChar:num+1]];
NSLog(@"CHAR:%@ and look for:%@ replace with:%@ results in %@",chars,[self convertDecimalToChar:num],[self convertDecimalToChar:num+1], replaced);
在NSMutableString
使用的情况下replaceCharactersInRange:withString:
[chat replaceCharactersInRange:[self convertDecimalToChar:num]withString:[self convertDecimalToChar:num+1];
或者replaceOccurrencesOfString:withString:options:range:
如果您需要更多选项,例如不区分大小写的替换:
NSRange range = NSMakeRange(0, [char length]);
[char replaceOccurrencesOfString:[self convertDecimalToChar:num] withString:[self convertDecimalToChar:num+1] options:NSCaseInsensitiveSearch range:range];
NSLiteralSearch
如果您还想替换以匹配给定字符串的大小写,则可能需要使用搜索选项。