字符串的构造方法
1.String(byte[] bytes) 通过字节数组 来创建
byte[] b = {97,98,99}; //65 : A
String s1 = new String(b);
System.out.println(s1);
2.String(byte[] bytes, int offset, int length)
String s2 = new String(b,1,2);
System.out.println(s2);
bytes - 要解码为字符的 byte
offset - 要解码的第一个 byte 的索引
length - 要解码的 byte 数
3.String(char[] value) 需要传递一个字符类型的数组
//字符是可以是中文
char[] c = {'明','就','周','六','但','是','还','要','上','课'};
String s3 = new String(c);
System.out.println(s3);
4.String(char[] value, int offset, int count)
String s4 = new String(c,3,4);
System.out.println(s4);
网友评论