No more nonsense to say, directly to everyone paste code.
One, code.
-(void) viewdidload
{
[super viewdidload];
Do no additional setup after loading the view.
The first method
NSLog (@ "--first-%i", [Self converttoint:@ "123 I Love You"]);
The second method
NSLog (@ "--second--%ld", [Self gettoint:@ "123 I Love You"]);
}
Get the Chinese and English mixed string length method 1
-(int) Converttoint: (nsstring*) strtemp
{
int strlength = 0;
char* p = (char*) [strtemp cstringusingencoding:nsunicodestringencoding];
for (int i=0; i<[strtemp lengthofbytesusingencoding:nsunicodestringencoding]; i++) {
if (*p) {
p++;
strlength++;
}
else {
p++;
}
}
return strlength;
}
To get the Chinese and English mixed string length method 2
-(Nsinteger) Gettoint: (nsstring*) strtemp
{
nsstringencoding enc = Cfstringconvertencodingtonsstringencoding (kcfstringencodinggb_18030_2000);
nsdata* da = [strtemp Datausingencoding:enc];
return [da length];
}
Second, output.
Two methods for judging the character length of Chinese and English mixed in the 2015-10-19 15:36:43.730 [9311:234111]--first-9
2015-10-19 15:36:43.736 The two ways to determine the length of characters in Chinese and English languages [ 9311:234111]--second--9
The above content is small to introduce the iOS in the judge in English and Chinese character length of the two methods, I hope to help!