Re: Adding methods to NSString
Re: Adding methods to NSString
- Subject: Re: Adding methods to NSString
- From: Alastair Houghton <email@hidden>
- Date: Thu, 27 Nov 2003 16:08:29 +0000
On 27 Nov 2003, at 14:52, Ed Watkeys wrote:
>
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.)
Sadly crossed fingers don't guarantee correctness (perhaps because you
can't type at the same time ;->). Your code will barf immediately when
it tries to access element -1 of the string.
Also, your code (like the other code I've seen in this thread) won't
work with composed character sequences. The other thing worth saying
is that it's probably slower to start with the string and remove
characters than it is to build-up another string containing the
characters you want (it's likely to involve a greater amount of
copying).
The following might be better (if somewhat longer):
@implementation NSString (RemovalOfCharsInSet)
/* Remove all characters from the specified set */
- (NSString *)stringByRemovingCharactersInSet:(NSCharacterSet *)charSet
options:(unsigned)mask
{
NSRange range;
NSMutableString *newString = [NSMutableString string];
unsigned len = [self length];
mask &= ~NSBackwardsSearch;
range = NSMakeRange (0, len);
while (range.length) {
NSRange substringRange;
unsigned pos = range.location;
range = [self rangeOfCharacterFromSet:charSet options:mask
range:range];
if (range.location == NSNotFound)
range = NSMakeRange (len, 0);
substringRange = NSMakeRange (pos, range.location - pos);
[newString appendString:[self substringWithRange:substringRange]];
range.location += range.length;
range.length = len - range.location;
}
return newString;
}
/* Remove all characters from the specified set, default options */
- (NSString *)stringByRemovingCharactersInSet:(NSCharacterSet *)charSet
{
return [self stringByRemovingCharactersInSet:charSet options:0];
}
/* Remove just the specified character */
- (NSString *)stringByRemovingCharacter:(unichar)character
{
NSCharacterSet *charSet = [NSCharacterSet
characterSetWithRange:NSMakeRange (character, 1)];
return [self stringByRemovingCharactersInSet:charSet];
}
@end
Of course, just like all the other examples in this thread, this was
typed in Mail.app, so there might be bugs/typos :-(
Kind regards,
Alastair.
[demime 0.98b removed an attachment of type application/pkcs7-signature which had a name of smime.p7s]
_______________________________________________
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.