1

私はC#で書かれた関数を持っています、それをobjective-cに変換したいと思います。どうやってするの?

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

2 に答える 2

2

コア基盤に頼ることなく:

#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;
}
于 2013-03-01T10:34:36.917 に答える
1

CFStringTransformリストからほとんどすべての変換を行うCoreFoundation 関数を使用できます。"đ" と "Đ" のみを個別に処理する必要があります。

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

例:

NSString *input = @"Hễllö Wõrld! - ếểễệđìíỉĩịòó";
NSString *output = UnicodeUnsign(input);
NSLog(@"%@", output);
// Output: Hello World! - eeeediiiiioo
于 2013-03-01T09:51:35.343 に答える