2013-02-28 30 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];