java编写函数,从一个字符串中按字节数截取一部分,但不能截取出半个中文(GBK码表)
1个回答
展开全部
class SplitString {
private String str;
private int byteNum;
public SplitString() {}
public SplitString(String str, int byteNum)
{
this .str = str;
this .byteNum = byteNum;
}
public void splitIt()
{
byte bt[] = str.getBytes();
System.out.println( " Length of this String ===> " + bt.length);
if (byteNum >= 1 )
{
if (bt[byteNum] < 0 )
{
String substrx = new String(bt, 0 , -- byteNum);
System.out.println(substrx);
} else
{
String substrex = new String(bt, 0 ,byteNum);
System.out.println(substrex);
}
} else
{
System.out.println( " 输入错误!!!请输入大于零的整数: " );
}
}
}
public class TestSplitString
{
public static void main(String args[])
{
String str = " 我ABC汉DEF " ;
int num = 6 ;
SplitString sptstr = new SplitString(str,num);
sptstr.splitIt();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询