我有一个NSMutableString
@"hello"
。 我想在第二位置与“A”,使其读取替换字符,“E” @"hallo"
。 我怎么做?
我曾经尝试这样做,以实现移位密码,但它抛出一个IndexOutBoundsException
- (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;
}