Truncate strings by byte

Source: Internet
Author: User

 

 

Public class demo {public static void main (string [] ARGs) {for (INT I = 0; I <15; I ++) {system. out. print ("truncated to" + I + "bit:"); splitstr ("I zwr love Java", I) ;}} public static void splitstr (string STR, int bytes) {int end = 0; For (INT I = 0, Len = Str. length (); I <Len; I ++) {If (ischinesechar (Str. charat (I) {bytes --;} bytes --; If (Bytes> = 0) {end = I + 1;} else {break;} system. out. println (Str. substring (0, end);} Private Static Boolean ischinesechar (char c) {return string. valueof (c ). getbytes (). length> 1;} // capture to 0th bits: // capture to 1st bits: // capture to 2nd bits: I // capture to 3rd bits: I z // capture to 4th bits: I zw // capture to 5th bits: I zwr // capture to 6th bits: I zwr // capture to 7th bits: I love zwr // capture to 8th: I love zwr J // capture to 9th: I love zwr ja // capture to 10th: I love Jav zwr // capture to 11th bits: I love Java zwr // capture to 12th bits: I love Java zwr // capture to 13th bits: I love Java in zwr. // I love Java in zwr}

 

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.