IOS-how to return the pinyin mnemonic code of a string, ios-pinyin

Source: Internet
Author: User

IOS-how to return the pinyin mnemonic code of a string, ios-pinyin

I also modified the sample code on the Internet based on it. Because projects will be used, I believe many people will also use projects. After implementation, we will share it with you, hoping that it will not take time for later users.

Tip: the regular expression is used here, and a third-party open-source component: RegexKitLite (how to use this component is a lot of online tutorials)

 

# Pragma mark-get the first letter of Pinyin (input Chinese character string, return the first letter of lowercase pinyin)

+ (NSString *) pinYingFirstCharactorStr :( NSString *) aString

{

// Converted to a variable string

NSMutableString * str = [NSMutableString stringWithString: aString];

// First convert to PinYin with tones

CFStringTransform (CFMutableStringRef) str, NULL, kCFStringTransformMandarinLatin, NO );

// Convert to PinYin without tone

CFStringTransform (CFMutableStringRef) str, NULL, kCFStringTransformStripDiacritics, NO );

// Convert to lowercase pinyin

NSString * pinYin = [str lowercaseString];

// Converts a pinyin string to an array of substrings Based on the specified regular expression.

NSString * regexString = @ "\ s"; // The regular symbol of the split interval used is a space.

NSArray * aArray = [pinYin componentsSeparatedByRegex: regexString]; // split

// Traverse the string array and splice the first letter

NSString * strReturn;

NSString * strProc = @"";

For (int I = 0; I <aArray. count; I ++ ){

// Numbers may be processed for substrings.

NSString * strTmp = [CRGOtherHelper firstReplaceInNumberStr: aArray [I];

StrReturn = [strProc stringByAppendingString: strTmp];

StrProc = strReturn;

}

// Obtain and return the first letter

Return strReturn;

}

 

// Split the input string into a new sub-array by numerical interval. The non-empty members in the sub-array are given the first letter to obtain a temporary character.

// Use this temporary character to replace the corresponding part of the character string in the original array member (the search method is to compare the previously split Member)

+ (NSString *) firstReplaceInNumberStr :( NSString *) waitProcStr

{

NSString * strResult = nil;

NSString * strWait = [[waitProcStr copy] autorelease];

// Find a string

NSString * regexString = @ "[0-9]";

 

// Further split into substrings Array

NSArray * strArray = [waitProcStr componentsSeparatedByRegex: regexString]; // split

NSArray * strCopy = [strArray copy];

For (int j = 0; j <strArray. count; j ++ ){

If (! [(NSString *) strArray [j] isEqualToString: @ ""])

{

// First letter

NSString * fistChar = [strArray [j] substringToIndex: 1];

// Partially replace

StrResult = [strWait stringByReplacingOccurrencesOfRegex: strCopy [j] withString: fistChar];

StrWait = strResult;

}

}

Return strResult;

}

Hope everyone can use it!

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.