Chinese and English mixed strings intercept Java

Source: Internet
Author: User

//intercept string Length (Chinese 2 bytes, half of Chinese display one)     PublicString subtextstring (String str,intLen) {        if(Str.length () &LT;LEN/2)returnstr; intCount = 0; StringBuffer SB=NewStringBuffer (); String[] SS= Str.split ("");  for(inti=1;i<ss.length;i++) {Count+=ss[i].getbytes (). length>1?2:1;            Sb.append (Ss[i]); if(Count>=len) Break; }        //no need to show ... Can be directly return sb.tostring ();        return(Sb.tostring (). Length () <str.length ())? Sb.append ("..."). toString (): STR; }

Many cases of the page will encounter the case to intercept the string, but the Chinese and English in the length of the difference will be caused by the mixed interception effect in English and Mandarin is better

Excerpt from blog http://happysoul.iteye.com/blog/1786408

Chinese and English mixed strings intercept Java

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.