当前位置:网站首页>String类的常用方法

String类的常用方法

2022-08-10 21:25:00 黑白不纯

一、String的构造方法

1.

public class Text {
    public static void main(String[] args) {
        String s = "abc";
        byte[] bytes= {97,98,99};

        String s1 = new String(bytes);
        System.out.println(s);//结果为abc,说明已经重写了toString方法

        System.out.println(s1);//自动将byte数组转换为字符串

    }
}

2.

public class Text {
    public static void main(String[] args) {
        //将char数组的一部分转化为字符串
        char[] chars = {'我','的','天'};
        String s1 = new String(chars,1,2);
        System.out.println(s1);

    }
}

原网站

版权声明
本文为[黑白不纯]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_49004189/article/details/126253781