java文字列の切り取り例の詳細


java文字列の切り取り例の詳細
テーマ
javaでは、文字列「abcd」と文字列「abこんにちは」の長さは同じで、四つの文字です。
ただし、対応するバイト数が異なり、漢字1文字が2バイトを占める。
一つの方法を定義し、指定されたバイト数でサブストリングをとる。
例えば、「abこんにちは」に対して、バイトを三つ取ると、串がabとあなたの字の半分になるので、半分は捨てます。
4バイトを取れば「abあなた」です。5バイトを取ればまだ「abあなた」です。
GBKとutf-8符号化のみを考慮する。
インスタンスコード:

import java.io.UnsupportedEncodingException;

import org.junit.Test;

/**
 * @author<a href="mailto:[email protected]" rel="external nofollow" >   </a>
 * @version 2017-4-4   1:08:45
 * @fileName StringCut.java
 */
public class StringCut {

  @Test
  public void analyze(){
    String str1 = "  abc";
    byte[] bs1=null;
    byte[] bs2=null;
    try {
       bs1 = str1.getBytes("GBK");
       System.out.println("---GBK---");
       for(byte b:bs1){
         System.out.print(b+" ");
       }
       System.out.println();
      //-60 -29 -70 -61 97 98 99 
      //     , gbk                  0    
       bs2 = str1.getBytes("utf-8");
       System.out.println("---utf-8---");
       for(byte b:bs2){
         System.out.print(b+" ");
       }
      //-28 -67 -96 -27 -91 -67 97 98 99 
      //     , utf-8                 0     
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }
  }
  /**
   *   :  len    ,  2           ,            
   * @param str
   * @param len
   * @return
   */
  private static String StringCutByGBK(String str,int len){
    byte[] bs = null;
    try {
      int count = 0;
      bs = str .getBytes("GBK");
      for(int i=len-1;i>=0;i--){
        if(bs[i]<0){
          count++;
        }else{
          break;
        }
        // 0  1  2  3  4 5  6 7  8  9  10 11 12  
      }  //-60 -29 -70 -61 -80 -95 97 98 99 -76 -17 -72 -25 
      if(count%2==0){
        String s=new String(bs, 0, len, "GBK");
        System.out.println("  "+len+"   :"+s);
      }else{
        String s=new String(bs, 0, len-1, "GBK");
        System.out.println("  "+len+"   :"+s);
      }
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }

    return null;
  }
  /**
   *   :  len    ,  3           ,         count%3   
   * @param str
   * @param len
   * @return
   */
  private static String StringCutByUTF8(String str,int len){
    byte[] bs = null;
    try {
      int count = 0;
      bs = str .getBytes("UTF-8");
      for(int i=len-1;i>=0;i--){
        if(bs[i]<0){
          count++;
        }else{
          break;
        }
      }  
      // 0  1  2  3  4  5  6 7 8 9  10 11 12
      //-60 -29 -70 -61 -80 -95 97 98 99 -76 -17 -72 -25 
      if(count%3==0){
        String s=new String(bs, 0, len, "UTF-8");
        System.out.println("  "+len+"   :"+s);
      }else{
        String s=new String(bs, 0, len-count%3, "UTF-8");
        System.out.println("  "+len+"   :"+s);
      }
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }

    return null;
  }
  @Test
  public void TEST() {
    String str = "   abc  ";
    try {
      System.out.println("---  gbk---");
      byte bs [] = str.getBytes("GBK");
      for(int i=0;i<=bs.length;i++){
        //System.out.print(bs[i]+" ");
        StringCutByGBK(str,i);

      }

      System.out.println("---  UTF-8---");
      byte bs2 [] = str.getBytes("utf-8");
      for(int i=0;i<=bs2.length;i++){
        //System.out.print(bs[i]+" ");
        StringCutByUTF8(str,i);

      }
    } catch (UnsupportedEncodingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

  }

}

疑問があれば、メッセージをお願いします。あるいは、当駅のコミュニティで交流して討論してください。ありがとうございます。