Tip: Regular expressions are used here, using a third-party open source component: Regexkitlite (how the component uses many online tutorials)
#pragma mark-Get pinyin initials (incoming kanji string, return lowercase pinyin first letter)
+ (NSString *) Pinyingfirstcharactorstr: (NSString *) astring
{
Turns into a mutable string
nsmutablestring *str = [nsmutablestring stringwithstring:astring];
Convert to pinyin with tones first
Cfstringtransform ((cfmutablestringref) str,null, kcfstringtransformmandarinlatin,no);
Convert to pinyin with no tones
Cfstringtransform ((cfmutablestringref) str,null, kcfstringtransformstripdiacritics,no);
Convert to lowercase pinyin
NSString *pinyin = [str lowercasestring];
Converts a phonetic string into an array of substrings based on a specified regular expression
NSString *regexstring = @ "\\s"; The regular symbol for the split interval used is a space
nsarray* Aarray=[pinyin componentsseparatedbyregex:regexstring]; To split
Iterate through an array of strings, stitching the initials
nsstring* Strreturn;
nsstring* Strproc = @ "";
for (int i=0; i<aarray.count; i++) {
There may be digital further processing for substrings
nsstring* Strtmp=[crgotherhelper Firstreplaceinnumberstr:aarray[i]];
Strreturn=[strproc Stringbyappendingstring:strtmp];
Strproc=strreturn;
}
Gets and returns the first letter
return strreturn;
}
Splits the passed-in string into a new sub-array at a numeric interval, and the non-null member in the sub-array takes the first letter to get a temporary character.
This temporary character is then used to replace the corresponding part of the string in the original array member (the lookup method is more than the previously split member)
+ (nsstring*) Firstreplaceinnumberstr: (nsstring*) Waitprocstr
{
nsstring* Strresult=nil;
nsstring* strwait=[[waitprocstr copy] autorelease];
Find string
NSString *[email protected] "[0-9]";
Further splitting into a substring array
nsarray* Strarray=[waitprocstr componentsseparatedbyregex:regexstring]; To split
nsarray* Strcopy=[strarray Copy];
for (int j=0; j<strarray.count; J + +) {
if (![ (nsstring*) strarray[j] isequaltostring:@ ""])
{
Take the first letter
nsstring* Fistchar=[strarray[j] substringtoindex:1];
Partial replacement
Strresult=[strwait Stringbyreplacingoccurrencesofregex:strcopy[j] Withstring:fistchar];
Strwait=strresult;
}
}
return strresult;
}
I hope we can use it!
ios-How to return phonetic help codes for a string