Use CFStringTransform to convert Chinese characters to PinYin (excluding tones) and cfstringtransform

Source: Internet
Author: User

Use CFStringTransform to convert Chinese characters to PinYin (excluding tones) and cfstringtransform

Usage:

NSMutableString * source = [sourceString mutableCopy];

CFStringTransform (_ bridge CFMutableStringRef) source, NULL, kCFStringTransformMandarinLatin, NO );

CFStringTransform (_ bridge CFMutableStringRef) source, NULL, kCFStringTransformStripDiacritics, NO );

Return source;

In this method, kCFStringTransformMandarinLatin converts Chinese characters to pinyin, and then calls kCFStringTransformStripDiacritics to remove the tone,

The mistake I made here was not to write both lines, but to write only one line, so it would have been the first line, and only the pinyin with tone would have been transferred, only write the second row and only return Chinese characters,

Later, my leadership optimized the method, because some words will be converted into incorrect pinyin, so the complete method is as follows and sorting is supported,

-(NSString *) phonetic :( NSString *) sourceString {

If ([sourceString isEqualToString: @ ""]) {

Return sourceString;

}

NSMutableString * source = [sourceString mutableCopy];

CFStringTransform (_ bridge CFMutableStringRef) source, NULL, kCFStringTransformMandarinLatin, NO );

CFStringTransform (_ bridge CFMutableStringRef) source, NULL, kCFStringTransformStripDiacritics, NO );

If ([[(NSString *) sourceString substringToIndex: 1] compare: @ "long"] = NSOrderedSame)

{

[Source replaceCharactersInRange: NSMakeRange (0, 5) withString: @ "chang"];

}

If ([[(NSString *) sourceString substringToIndex: 1] compare: @ "shen"] = NSOrderedSame)

{

[Source replaceCharactersInRange: NSMakeRange (0, 4) withString: @ "shen"];

}

If ([[(NSString *) sourceString substringToIndex: 1] compare: @ ""] = NSOrderedSame)

{

[Source replaceCharactersInRange: NSMakeRange (0, 3) withString: @ "xia"];

}

If ([[(NSString *) sourceString substringToIndex: 1] compare: @ ""] = NSOrderedSame)

{

[Source replaceCharactersInRange: NSMakeRange (0, 3) withString: @ "di"];

}

If ([[(NSString *) sourceString substringToIndex: 1] compare: @ "weight"] = NSOrderedSame)

{

[Source replaceCharactersInRange: NSMakeRange (0, 5) withString: @ "chong"];

}

Return source;

}


How can we combine the tones of Chinese characters and pinyin?

... I doubt if you are Chinese.

I would like to express my gratitude to the software for converting Chinese characters to pinyin, which can be used as a Chinese character or for converting Chinese characters online.

.

Hello!

The following address can be converted, and multiple words can be converted together. Hope to help you!

Hanyu.iciba.com/pinyin

Have a good time!
.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.