• 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: Ignore accents when comparing strings
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Ignore accents when comparing strings


  • Subject: Re: Ignore accents when comparing strings
  • From: Dustin Voss <email@hidden>
  • Date: Thu, 6 Jan 2005 12:26:56 -0800

On 4 Jan, 2005, at 10:52 PM, Simon alias Trax wrote:

When I compare two NSStrings, I want the accented characters be treated as regular characters. For example, if "é" is found in a string, it will act like "e" and "ê" and so on. Another example : "parle" and "parlé" will be considered the same when compared.

I'm doing this, through the method sortUsingSelector:@selector(compare:) (I can't use anything else since I don't know how to pass parameters within this method.

Here's a real-life example :

NSString *mot1 = @"arc";
NSString *mot2 = @"a";
NSString *mot3 = @"à";
NSMutableArray *sim = [NSMutableArray arrayWithObjects:mot1,mot2,mot3,nil];
NSLog(@"%@",sim);
NSLog(@"---");
[sim sortUsingSelector:@selector(compare:)];
NSLog(@"%@",sim);


The logs show me what happened. "à" is supposed to be equivalent to "a", not after, not before.

So the correct sorting should be, in ascending order : "a","à","arc". But the computer sort of thinks "à" as the second letter of the alphabet. So I get the wrong result : "a","arc","à".

Use the Carbon Unicode Utilities to do the comparison. It orders these strings properly, and you can set it up to ignore diacriticals.


The API reference is at <http://developer.apple.com/documentation/Carbon/Reference/ Unicode_Utilities_Ref/uu_refchap/FunctionGroupIndex.html>.

Here's some code I used to check it out:

#import <Foundation/Foundation.h>
#import <Carbon/Carbon.h>

int collatecompare (id a, id b, void* context)
{
NSData *da = [a dataUsingEncoding: NSUnicodeStringEncoding];
NSData *db = [b dataUsingEncoding: NSUnicodeStringEncoding];

SInt32 order;
OSStatus err = UCCompareTextDefault (0, [da bytes], [a length], [db bytes], [b length], NULL, &order);
if (err) NSLog (@"Error in comparison");

if (order < 0) return NSOrderedAscending;
if (order > 0) return NSOrderedDescending;
return NSOrderedSame;
}


int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

NSString *mot1 = @"ar";
NSString *mot2 = @"a";
unichar u[] = {0x00E0};
NSString *mot3 = [NSString stringWithCharacters: u length: 1];

NSMutableArray *sim = [NSMutableArray arrayWithObjects: mot1, mot2, mot3, nil];

NSLog([sim componentsJoinedByString: @", "]);
[sim sortUsingFunction:collatecompare context:NULL];
NSLog([sim componentsJoinedByString: @", "]);

[pool release];

return 0;
}




_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Ignore accents when comparing strings (From: "Simon alias Trax" <email@hidden>)

  • Prev by Date: Re: #ifdef/build style question
  • Next by Date: Re: #ifdef/build style question
  • Previous by thread: Re: Ignore accents when comparing strings
  • Next by thread: Why does this not compile?
  • Index(es):
    • Date
    • Thread