IOS – Unicode Unsign

I have a function written in C#, i want to convert it to objective-c. How to do it?

public static string UnicodeUnSign(string s)
{
    const string uniChars = "àáảãạâầấẩẫậăằắẳẵặèéẻẽẹêềếểễệđìíỉĩịòóỏõọôồốổỗộơờớởỡợùúủũụưừứửữựỳýỷỹỵÀÁẢÃẠÂẦẤẨẪẬĂẰẮẲẴẶÈÉẺẼẸÊỀẾỂỄỆĐÌÍỈĨỊÒÓỎÕỌÔỒỐỔỖỘƠỜỚỞỠỢÙÚỦŨỤƯỪỨỬỮỰỲÝỶỸỴÂĂĐÔƠƯ";
    const string koDauChars = "aaaaaaaaaaaaaaaaaeeeeeeeeeeediiiiiooooooooooooooooouuuuuuuuuuuyyyyyAAAAAAAAAAAAAAAAAEEEEEEEEEEEDIIIOOOOOOOOOOOOOOOOOOOUUUUUUUUUUUYYYYYAADOOU";

    if (string.IsNullOrEmpty(s))
    {
        return s;
    }

    string retVal = String.Empty;
    for (int i = 0; i < s.Length; i++)
    {
        int pos = uniChars.IndexOf(s[i].ToString());
        if (pos >= 0)
            retVal += koDauChars[pos];
        else
            retVal += s[i];
    }
    return retVal;
}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Without resorting to core foundation:

#import <Foundation/Foundation.h>

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

   NSString *unicodeCharacters = @"àáảãạâầấẩẫậăằắẳẵặèéẻẽẹêềếểễệđìíỉĩịòóỏõọôồốổỗộơờớởỡợùúủũụưừứửữựỳýỷỹỵÀÁẢÃẠÂẦẤẨẪẬĂẰẮẲẴẶÈÉẺẼẸÊỀẾỂỄỆĐÌÍỈĨỊÒÓỎÕỌÔỒỐỔỖỘƠỜỚỞỠỢÙÚỦŨỤƯỪỨỬỮỰỲÝỶỸỴÂĂĐÔƠƯ";

   NSString *decomposed = [unicodeCharacters decomposedStringWithCanonicalMapping];
   NSLocale *usLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease];

   NSString *cleaned = [decomposed stringByFoldingWithOptions:NSDiacriticInsensitiveSearch locale:usLocale];
   cleaned = [cleaned stringByReplacingOccurrencesOfString:@"đ" withString:@"d"];
   cleaned = [cleaned stringByReplacingOccurrencesOfString:@"Đ" withString:@"D"];
   NSLog (@"%@", cleaned);

   [pool drain];
   return 0;
}

Method 2

You could use the CoreFoundation CFStringTransform function which does almost all transformations from your list. Only “đ” and “Đ” have to be handled separately:

NSString *UnicodeUnsign(NSString *s)
{
    NSMutableString *result = [s mutableCopy];
    // __bridge only required if you compile with ARC:
    CFStringTransform((__bridge CFMutableStringRef)result, NULL, kCFStringTransformStripCombiningMarks, NO);

    [result replaceOccurrencesOfString:@"đ" withString:@"d" options:0 range:NSMakeRange(0, [result length])];
    [result replaceOccurrencesOfString:@"Đ" withString:@"D" options:0 range:NSMakeRange(0, [result length])];

    return result;
}

Example:

NSString *input = @"Hễllö Wõrld! - ếểễệđìíỉĩịòó";
NSString *output = UnicodeUnsign(input);
NSLog(@"%@", output);
// Output: Hello World! - eeeediiiiioo


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x