Re: Adding methods to NSString
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.