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!