2013-02-28 57 views

回答

0
NSMutableString *a = [NSMutableString stringWithString:@"HOUSE" ]; 

    NSString *b= @"MUSIC"; 

[a replaceCharactersInRange:NSMakeRange(0, 1) withString:[b substringWithRange:NSMakeRange(0, 1)]]; 

當你得到一個結果MOUSE ;-)

0

如果你正在尋找的方法是這樣的:setCharacter:atIndex:

然後看下面的代碼:

NSMutableString *string=[NSMutableString stringWithString:@"abcdefgh"]; 

[string setCharacter:'X' atIndex:0]; //Note X is character not string so "X" and its is primitive therefore even @ is not prefixed. 

NSLog(@"%@",string); 

這通過NSMutableString的類別獲得,其方法實施

@implementation NSMutableString (SetCharacterAtIndex) 

- (void)setCharacter:(char)character atIndex:(NSUInteger)index;{ 

    [self replaceCharactersInRange:NSMakeRange(index, 1) withString:[NSString stringWithFormat:@"%c",character]]; 

} 

編輯:

具有上述方法之後,可以使用:

[一個characterAtIndex:0] = [B characterAtIndex:0];

[a setCharacter:[b characterAtIndex:0] atIndex:0];