Chinese and English characters

Source: Internet
Author: User

Use Perl to intercept a string that contains Chinese characters, English letters, numbers, and symbols. It also contains Chinese Punctuation Marks. After intercept a certain number of characters, the Chinese character is complete, no garbled characters or question marks will appear.
Sub makelen ($)
{
My ($ SSTR, $ slen) = @_;
My $ makelen, $ parity;
$ Mslen = length ($ SSTR );
If ($ mslen> $ slen)
{
For ($ Mn = 0; $ Mn <$ slen; $ Mn ++) # within the required length
{
$ Ss = substr ($ SSTR, $ Mn, 1 );
If (ord ($ SS) <127) # English
{
$ Parity + = 1;
}
}
If ($ parity % 2 = 1) # contains an odd number of English characters
{
If ($ slen % 2 = 1) # If the required length is also odd
{
$ Tmpstr = substr ($ SSTR, 0, $ slen );
}
Else # The required length is an even number.
{
$ Tmpstr = substr ($ SSTR, 0, $ slen-1 );
}
}
Else # contains an even number of English characters or an even number of characters ranging from 0 to 0.
{
If ($ slen % 2 = 1) # If the required length is odd
{
$ Tmpstr = substr ($ SSTR, 0, $ slen-1 );
}
Else # The required length is an even number.
{
$ Tmpstr = substr ($ SSTR, 0, $ slen );
}
}
$ Makelen = $ tmpstr;
}
Else
{
$ Makelen = $ SSTR;
}
Return $ makelen;
}

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.