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 >= &NBSP;0X4E00&NBSP;&&&NBSP;UTFCODE&NBSP;<=&NBSP;0X9FA5)) return yes; else return no;}
iOS development determines whether the first character of a string is Chinese or begins with a letter