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; } 
+1
source share
2 answers

You can use the CoreFoundation CFStringTransform function, which performs almost all the conversions from your list. Only "ฤ‘" and "ฤ" need to be processed 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 
+1
source

Without resorting to the main 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; } 
+2
source

Source: https://habr.com/ru/post/1489213/


All Articles