import java.io.IOException;
import java.util.*;
public class CutByte
{
public static void main(String[] args) throws IOException
{
System.out.print("请输入一串字符串: ");
//scanner输入字符串
Scanner s = new Scanner(System.in);
String s1 = s.nextLine();
System.out.print("\n");
//把输入的字符串按照规定编码方式变成数组
byte[] b = s1.getBytes("gb2312");
int len = s1.getBytes("gb2312").length;
System.out.println("输入字符串的长度为: "+ len + "个字节");
System.out.print("请输入期望输出字符串的长度: ");
int p = s.nextInt();
System.out.print("\n");
System.out.print("字符串的前" + p + " 个字节为: ");
String stringOut = cutString( b , p);
System.out.println(stringOut);
}
/**
*如果最后一个字节是汉字的一半,那么舍去,不输出
*@param a 字符串按照规定的编码方式转换成数组,j 数组的长度,
* k 按字节截取k个字节
*@return 返回前面k个字节组合而成的数组,
*String(byte[] bytes ,int offset ,int length ,String charset name)
*offset参数是数组的偏移量,即数组的而第一个字符的索引
*注意: utf-8 编码中一个汉字占用3个字节,
* gbk和gb2312编码方式一个汉字占用2个字节
*/
public static String cutString (byte[] a , int k) throws IOException
{
String s = "";
int han = 0 ;
for(int i = (k-1) ; i >= 0 ; i-- )
{
//"123你好"总共7个字节,截取前面7个,则han=4
//截取前面6个,han=3
//截取前面4个,han=1
if( a[i] < 0 )
{
han++;
}
else
{
break;
}
}
if( han % 2 == 0 )
{
s = new String( a , 0 , k ,"gb2312") ;
}
if( han % 2 == 1 )
{
s = new String( a , 0 , k-1 ,"gb2312") ;
}
return s;
}
}