How to judge the character length of Chinese and English mixed in IOS _ios

Source: Internet
Author: User
Tags mixed

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!

Related Article

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.