ios-How to return phonetic help codes for a string

Source: Internet
Author: User

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

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.