Objective c 替换字符串中的字符

Objective c 替换字符串中的字符,objective-c,string,cocoa,nsstring,Objective C,String,Cocoa,Nsstring,我有一个NSMutableString@hello。我想将第二个位置的字符“e”替换为“a”,这样它就会显示@hallo。我该怎么做 我已经尝试过实现移位密码,但它抛出了IndexOutBoundsException 您可以使用stringByReplacingOccurrencesOfString:withString:of。您可以使用stringByReplacingOccurrencesOfString:withString:of 另外,一定要学会使用在线参考资料 另外,一定要学会使用在线

我有一个NSMutableString@hello。我想将第二个位置的字符“e”替换为“a”,这样它就会显示@hallo。我该怎么做

我已经尝试过实现移位密码,但它抛出了IndexOutBoundsException

您可以使用stringByReplacingOccurrencesOfString:withString:of。

您可以使用stringByReplacingOccurrencesOfString:withString:of

另外,一定要学会使用在线参考资料


另外,一定要学会使用在线参考资料。

我明白了。只能在指定索引处替换字符吗?请改为使用“stringByReplacingOccurrencesOfString:withString:options:range:”。我明白了。只能在指定索引处替换字符吗?请改为使用“stringByReplacingOccurrencesOfString:withString:options:range:”。或者用作参考,它是免费的,使用Apple文档。或者用作参考,它是免费的,使用Apple文档。可能重复的可能重复
- (NSString*)encode:(NSString*)original withShift:(int)shift {

    NSMutableString* encoded = [NSMutableString stringWithString:original];
    for (int i=0; i < [encoded length]; i++) {
        char oriChar = [encoded characterAtIndex:i];
        if (oriChar == ' ') {
            continue;
        }
        char encChar = ((oriChar - LETTER_POS) + shift) % ALPHABET_LENGTH + LETTER_POS;

        NSRange range = {i, i};
        [encoded replaceCharactersInRange:range withString:[NSString stringWithUTF8String:&encChar]];

    }
    return encoded;

}
NSRange r = {1,1}; //String indexing is 0-based
[s replaceCharactersInRange: r withString:@"a"]