Re: Better way to replace a character in a string?
Re: Better way to replace a character in a string?
- Subject: Re: Better way to replace a character in a string?
- From: Ondra Cada <email@hidden>
- Date: Fri, 2 Aug 2002 17:06:26 +0200
On Thursday, August 1, 2002, at 06:37 , Tom Gray wrote:
Is there a better way to replace a single character in an NSMutableString
then this:
// Create an NSString from our character.
tempString = [NSString stringWithCString:&theCharacter length:1];
// Get the desired location for the character replacement.
range.location = desiredOffset;
range.length = 1;
// Replace the character in the terminal string with the new
character.
[myMutableString replaceCharactersInRange:range withString:tempString]
;
Well, yeah and not. The better way looks like this:
@interface NSMutableString (SuchThingsBelongToApi)
-(void)replaceCharacterAtIndex:(unsigned)i withCharacter:(unichar)uc;
@end
@implementation NSMutableString (SuchThingsBelongToApi)
-(void)replaceCharacterAtIndex:(unsigned)i withCharacter:(unichar)uc {
[self replaceCharactersInRange:NSMakeRange(i,1) withString:[[[NSString
alloc] initWithCharactersNoCopy:&uc length:1 freeWhenDone:NO] autorelease]
];
}
@end
---
Ondra Cada
OCSoftware: email@hidden
http://www.ocs.cz
private email@hidden
http://www.ocs.cz/oc
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.