String类的构造方法


public class StringDemo2 {
    public static void main(String[] args) {
        fun1();
    }
    /*
     *  Strign(char[] value)传递字符数组
     *  将字符数组转换为字符串  字符数组参数不查询编码表
     *  String(char[] valuie,int offeset,int count);
     *  offeset代表数组开始的索引、
     *  count 代表个数
     * */
    public static void fun1(){
        char[] ch = {'a','b','c','d','e','f'};
        String s= new String(ch);
        String s1= new String(ch,1,2);
        
        
        System.out.println(s1);
    }
    public static void fun() {
        byte[] bytes = {65,98,99,100,101};
        //调用String的构造方法  传递字节数组  字节转字符串(查unicode码)
        String str = new String(bytes);
        System.out.println(str);
        byte[] bytes1 = {97,98,99,100,101};
         //调用构造方法  传递数组  传递两个int下x,y   代表截取   截取的位置   x代表下标开始的位置  y代表结束位置
        String s = new String(bytes1,1,3);
        System.out.println(s);
    }
}

原文地址:https://www.cnblogs.com/lxy4/p/10554562.html