• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Better way to replace a character in a string?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.

References: 
 >Better way to replace a character in a string? (From: Tom Gray <email@hidden>)

  • Prev by Date: Re: Accessor methods and (auto)release <Memory trail>
  • Next by Date: Re: encodeConditionalObject:
  • Previous by thread: Re: Better way to replace a character in a string?
  • Next by thread: teaching a game to "speak" different languages
  • Index(es):
    • Date
    • Thread