Search code examples
iosnsstringchinese-locale

stringByReplacingOccurrencesOfString not work as expected when pinyin replace ǘ to v


I try to convert pinyin ǘ to v after

CFStringTransform((__bridge CFMutableStringRef) mutableString, NULL, kCFStringTransformToLatin, false);

but when

(lldb) po [@"uán" stringByReplacingOccurrencesOfString:@"ǘ" withString:@"v"]

the output is:

vn

uá eat disappear


Solution

  • Please use the below code, Why I thought this might work? I got some hint from How Swift String saves the unicode chars

    I still don't know how this worked, may be I need to read more about Obj-C strings especially how it saves the unicode chars

    NSString *text = @"uán";
    NSString *repStr = [text stringByReplacingOccurrencesOfString:@"ǘ" withString:@"v" options:NSLiteralSearch range:NSMakeRange(0, text.length)];
    NSLog(@"%@", repStr);
    

    Console logs

    TestObjc[1221:69730] uán