Java---Exercises (interview questions): String interception (2-final) __java

Source: Internet
Author: User

In Java, the string "ABCD" is the same length as the string "ab Hello", and is four characters.
But the corresponding number of bytes is different, one Chinese character occupies two bytes.
Defines a method that takes a substring by the specified number of bytes.
For example: for "ab Hello", if take three bytes, then the substring is AB and "You" word half, then half will abandon. If you take four bytes is "ab You", take Five bytes or "Ab you."

The above given is an intercept string under GBK encoding.
I've written a code that can intercept strings under both Utf-8 and GBK encodings.

Note: Most of the Chinese characters under Utf-8 are 3 bytes, so in order to simplify, they are all treated as 3 bytes.

Attention:
In the last, I misunderstood the question, in fact, the problem requires only the output of the first n-byte string can be.
In the previous I was splitting a string by n ....

Package Io.app;

Import java.io.IOException;

Import Org.junit.Test; /** * * @author Chen Haoxiang * * @version 1.0 2016-4-28/public class Stringcut {public static void main (string[] A
        RGS) {String str = "AB Hello a coffee is coffee is";
            byte bf[] = Str.getbytes ()//This is the default encoding, possibly GBK, or UTF-8 for (int i=0;i<=bf.length;i++) {String res;
                try {res = cutstring (str,i);
            System.out.println (i+ ":" +res);
            catch (IOException e) {e.printstacktrace ();
     }}/** * According to the incoming string, to determine what is encoded, respectively, to guide different methods * @param str * @param len * @return * @throws IOException */private static string cutstring (string str, int len) throws IOException {//sys
            Tem.getproperty ("file.encoding")---Obtain the system encoding if (System.getproperty ("file.encoding"). Equalsignorecase ("GBK")) {
        return CUTSTRINGGBK (str, len); } if (System.getproperty ("File.encoding "). Equalsignorecase (" Utf-8 ")) {return CutStringUtf8 (str, len);
    throw new RuntimeException ("does not support encoding for current system"); private static string CutStringUtf8 (string str, int len) throws IOException {byte buf[] = str.getbytes ("UT
        F-8 ");
        int count=0;
            for (int i=len-1;i>=0;i--) {if (buf[i]<0) {count++;
            }else{break;
        int x = count%3;
    return new String (Buf,0,len-x, "utf-8"); private static string CUTSTRINGGBK (string str, int len) throws IOException {byte buf[] = Str.getbytes ("GBK
        ");
        int count=0;
            for (int i=len-1;i>=0;i--) {if (buf[i]<0) {count++;
            }else{break;
        } if (count%2==0) {return new String (Buf,0,len, "GBK");
        }else{return new String (buf,0,len-1, "GBK");


   }
    } @Test/** * You can run a single method without needing the main method ....
        * @throws IOException */public void analyze () throws IOException {//string str = "AB Hello";
        String str = "AB Hello a coffee is coffee is coffee is";
        byte buf[] = Str.getbytes ("GBK");
        byte buf[] = Str.getbytes ("Utf-8");
        for (byte b:buf) {System.out.print (b + "");
    } System.out.println ();
 }


}

Results of operation under GBK:
(Chinese characters are 2 bytes)

0: 
1:a
2:ab
3:ab
4:ab you 5:ab you 6:ab Hello
7:ab
Hello a 8:ab hello a 9:ab hello a coffee is 10 : AB Hello a coffee is
11:ab Hello a coffee is coffee is

Results of operation under UTF-8:
(Chinese character comprehension is 3 bytes)

0: 
1:a
2:ab
3:ab
4:ab
5:ab
you 6:ab you 7:ab you 8:ab Hello 9:ab Hello a
  10:ab Hello a
11:ab hello a 12:ab hello a coffee is 13:ab hello a coffee is 14:ab hello a coffee is 15:ab
Hello a coffee is coffee is
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.