On 7 Oct 2008, at 7:46 pm, Gerriet M. Denkmann wrote:


On 7 Oct 2008, at 09:20, cGraham Cox <[EMAIL PROTECTED]> wrote:

On 7 Oct 2008, at 4:22 pm, Sandro Noel wrote:

i'm having a problem comparing some type of strings, for example the
one giving me a problem right now.
is let's say in my database i have 4 version of the same string .

"sandro's computer"
"sandro's_computer"
"sandros computer"
"sandros_computer"

I would like them to compare as being equal,
is there a way I can mingle with the comparing routine
to ask it to ignore some characters in the comparison?


First strip out the characters to be ignored:

NSString* tempString = [myString stringByTrimmingCharactersInSet:
[NSCharacterSet characterSetWithCharactersInString:@"'_ "]];

returns "sandroscomputer" given any of the above.

Are you quite sure?
The Tiger documentation says: "Returns a new string made by removing from both ends of the receiver characters contained in a given character set."

If this is still true, then the internal apostrophs would not get removed.

But maybe I am misunderstanding the documentation.
DId not actually test this.

Whoops, I made a mistake.

I thought I'd seen a method for stripping characters in a set from a string, and searching for it jumped on the stringByTrimming... method. But as you correctly state, that's not right! However, I wasn't going entirely mad, as I had seen the method, courtesy of a handy NSString category by Alastair Houghton. I hope he won't mind my copying it in below.

Apologies for the misleading info.

G.



@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;
}


- (NSString *) stringByRemovingCharactersInSet:(NSCharacterSet*) charSet
{
        return [self stringByRemovingCharactersInSet:charSet options:0];
}


- (NSString *)          stringByRemovingCharacter:(unichar) character
{
NSCharacterSet *charSet = [NSCharacterSet characterSetWithRange:NSMakeRange (character, 1)];
        
        return [self stringByRemovingCharactersInSet:charSet];
}

@end

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to