• 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: Adding methods to NSString
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Adding methods to NSString


  • Subject: Re: Adding methods to NSString
  • From: Ed Watkeys <email@hidden>
  • Date: Thu, 27 Nov 2003 09:52:08 -0500

On Nov 27, 2003, at 7:37 AM, Jvrn Salewski wrote:

@implementation NSString (NSStringExtensions)

-(NSString *)stringByRemovingCharacter:(unichar)thisChar {

int index;
NSMutableString* tempString = [NSMutableString stringWithString:self];

index = -1;

while(++index < [tempString length]) {
if([tempString characterAtIndex:index] == thisChar){
[LOOK-HERE]
[tempString deleteCharactersInRange:NSMakeRange(index,1)];
[/LOOK-HERE]

}
}
// if not simply returning the tempString:
return [NSString stringWithString:tempString];
}

Improvements aside, does this function even work properly? Shouldn't index only be incremented if a character is NOT deleted? If a character is found and deleted, then the next position to search is the same position as the last iteration through the loop. Correct? Here's my version:

@implementation NSString (NSStringExtensions)

-(NSString *)stringByRemovingCharacter:(unichar)thisChar {

int index;
NSMutableString* tempString = [NSMutableString stringWithString:self];

for(index = -1; index <= [tempString length]; } {
if([tempString characterAtIndex:index] == thisChar)
[tempString deleteCharactersInRange:NSMakeRange(index,1)];
else
index++;
}
// if not simply returning the tempString:
return [NSString stringWithString:tempString];
}

This version is also shorter, so it meets both correctness and aesthetic criteria. (Fingers crossed on the correctness part.)

Regards,
Ed
_______________________________________________
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.

  • Follow-Ups:
    • Re: Adding methods to NSString
      • From: Alastair Houghton <email@hidden>
References: 
 >Re: Adding methods to NSString (From: Jörn Salewski <email@hidden>)

  • Prev by Date: Re: intercepting f-key
  • Next by Date: Re: Is it possible to interact with Microsoft Office X programmatically
  • Previous by thread: Re: Adding methods to NSString
  • Next by thread: Re: Adding methods to NSString
  • Index(es):
    • Date
    • Thread