iOS development determines whether the first character of a string is Chinese or begins with a letter

Source: Internet
Author: User

     nsstring *titlestr = @ "Lin Xiao Xi";  //the first character          NSString *firstStr = [titleStr substringToIndex:1];                 //judge is not the beginning of Chinese         bool isfirst = [self iszhongwenfirst: firststr];        if  (isfirst != 0)  {            nslog (@ "The first character is the beginning of the Chinese--%@",  firststr);         }        else         {            // Determine if the letter starts with a             bool isa = [ self pipeizimu:firststr];            if  (isa != 0)  {             nslog (@ "The first one is the beginning of the letter--%@",  FIRSTSTR);             }             else             {                nslog (@ "--not the Chinese and the beginning of the letter--%@", Titlestr);                             }         }
#pragma  mark  Regular expressions/To determine whether the first method begins in Chinese-(BOOL) Pipeizimu: (nsstring *) str{    // Determine whether to start with a letter     nsstring *zimu = @ "/^[a-za-z]/";     nspredicate *regextesta = [nspredicate predicatewithformat:@ "SELF MATCHES %@",  ZIMU];        if  ([Regextesta evaluatewithobject:str]  == yes)     {        return YES;     }    else    {         return no;    }}-(BOOL) Iszhongwenfirst: (NSString *) firstStr{     //whether to start in Chinese (Unicode Chinese encoding Range is 0x4e00~0x9fa5)     int utfcode =  0;    void *buffer = &utfcode;    nsrange  range = nsmakeranGE (0, 1);     //judge is not the Chinese beginning of the,buffer-> get character byte data  maxlength->buffer maximum length   usedlength-> actual write length, do not need to pass the null encoding-> character encoding constant, different encoding method after the conversion of the byte length is not the same, here I used the Utf16 little-endian , the MaxLength is 2 bytes, and if Unicode is used, the 4 byte  options-> encoding conversion option is required, with two values, Nsstringencodingconversionallowlossy and nsstringencodingconversionexternalrepresentation range->, respectively. Gets the range of characters in the string, set here the first character  remainingRange-> the suggested fetch range, which can be passed null    bool b =  [firststr getbytes:buffer maxlength:2 usedlength:null encoding: Nsutf16littleendianstringencoding options:nsstringencodingconversionexternalrepresentation range: range remainingrange:null];    if  (b &&  (utfCode >=  0X4E00 && UTFCODE <= 0X9FA5))          return yes;    else         return no;} 


iOS development determines whether the first character of a string is Chinese or begins with a letter

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.