面试题,截取字符串数组

public class test001 {
public static void main(String[] args) throws Exception {
String str2 = “测ABC试DEF”;
splitString(str2,6);
}

public static String splitString(String str,int len)throws Exception{
    if (null==str){
        System.out.println("The string is null");
    }

    int byteNum =0;

    byte[] bt = str.getBytes();

    byteNum = bt.length;

    if (len>byteNum){
        len = byteNum;
    }

    if (bt[len]<0){
        str = new String(bt,0,--len);
        System.out.println(str);
    }else{
        str = new String(bt,0,len);
        System.out.println(str);
    }


    return str;

}

}

猜你喜欢

转载自blog.csdn.net/weixin_44162368/article/details/91410734
今日推荐