4

Hello i have a problem that i don't know how to solve. The problem is that I even don't know how to google about it. I've already spent hours on the internet finding the solution, but didn't succeeded. The situation is like this:

I have String, lets say:

NSString *string = @"aąbcčdeęėfghiįjzž";

my result has to be like this:

NSString *string = @"aabccdeeefghiujzz";

So if you understood i have to do this:

replace ą with a

replace č with c

replace ė with e

replace ę with e

and so on..

Maybe anyone could help me? i have an answer but it's not very optimized, i am hoping for some more convenient solution.

Thanks in advance!

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
Lukas
  • 2,515
  • 4
  • 28
  • 37
  • 2
    This might help you: http://stackoverflow.com/questions/1231764/nsstring-convert-to-pure-alphabet-only-i-e-remove-accentspunctuation – Luke May 24 '12 at 14:10
  • Thanks for your help! But Dave DeLong solution is the best that i saw. – Lukas May 25 '12 at 06:56

4 Answers4

11

Let the frameworks do the conversion for you:

NSString *blah = @"aąbcčdeęėfghiįjzž";
NSData *data = [blah dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *newblah = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"new: %@", newblah);

This logs:

new: aabccdeeefghiijzz
Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
  • Ugh… I like how easy that is, but wow does it feel like black magic. Is that behavior reliable? – Jeffery Thomas May 24 '12 at 20:33
  • @JefferyThomas according to the documentation, [this is why the method exists](http://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/doc/uid/20000154-dataUsingEncoding_allowLossyConversion_). So yes, I'd say it's reliable. :) – Dave DeLong May 24 '12 at 20:36
  • That is nice. I'm sorry, I should have RTFM'd before asking. – Jeffery Thomas May 24 '12 at 20:38
  • @JefferyThomas s'all good. the frameworks are huge, and it takes a long time to become familiar with all the fun little ins and outs. I keep finding neat little things all the time. – Dave DeLong May 24 '12 at 20:39
  • Incase anybody will step on this, i have found another, even muchmore easyier way. In my case i need to find if string consists another string ignoring the letters i specified, so here is how i've done it: `NSString *fullStr = item.infoText; NSRange range = [fullStr rangeOfString:search options:NSDiacriticInsensitiveSearch|NSCaseInsensitiveSearch]; if (range.location != NSNotFound) { NSLog(@"String found!"); }` – Lukas Jul 20 '12 at 10:36
5

This will work:

NSString *str = @"aąbcčdeęėfghiįjzž";
NSLog(@"%@", str);
NSMutableString *s = [[str decomposedStringWithCompatibilityMapping] mutableCopy];
NSUInteger pos = 0;
while(pos < s.length) {
    NSRange r = [s rangeOfComposedCharacterSequenceAtIndex:pos];
    if (r.location == NSNotFound) break;
    pos = ++r.location;
    if (r.length == 1) continue;
    r.length--;
    [s deleteCharactersInRange:r];
}
NSLog(@"%@", s);

The idea is to first decompose each character with diacritical mark into a sequence of its base character and a sequence of Unicode combining diacritical marks (that's done by decomposedStringWithCompatibilityMapping), then go through the string, and remove all such marks one by one (that's what the loop does).

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

Dave DeLong answer will replace "æ" with "ae" and "ß" with "s", but won't replace ligatures œ, ij, ff, fi, fl, ffi, ffl, ſt, st, ...

An improved solution is to add three lines of mapping to handle everything fine:

blah = [blah stringByReplacingOccurrencesOfString:@"Œ" withString:@"OE"];
blah = [blah stringByReplacingOccurrencesOfString:@"œ" withString:@"oe"];
blah = [blah precomposedStringWithCompatibilityMapping];

NSData *data = [blah dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *newblah = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
Cœur
  • 37,241
  • 25
  • 195
  • 267
-1

I can offer only this:

[string stringByReplacingOccurrencesOfString:@"ą" withString:@"a"];

and so on...

Moonkid
  • 881
  • 7
  • 17