String类概述及其构造方法
程序员文章站
2022-07-13 21:17:53
...
一、概述
package cn.test;
import java.util.Scanner;
public class studentTest {
public static void main(String[] args) {
String s1=new String();
System.out.println("s1:"+s1);
System.out.println(s1.length());
}
}
结果:
s1:
0
二、几种构造方法
//public String();空构造
String s1=new String();
System.out.println("s1:"+s1);
System.out.println("s1.length():"+s1.length());
//public String(byte[] bytes);把字节数组转换为字符串
byte[] bytes={97,98,99,100,101};
String s2=new String(bytes);
System.out.println("s2:"+s2);
System.out.println("s2.length():"+s2.length());
//public String(byte[] bytes,int index,int length);把字节数组的一部分转换为字符串
String s3=new String(bytes,1,3);
System.out.println("s3:"+s3);
System.out.println("s3.length():"+s3.length());
//public String(char[] value);把字符数组转换为字符串
char[] ch={'a','b','c','d','e'};
String s4=new String(ch);
System.out.println("s4:"+s4);
System.out.println("s4.length():"+s4.length());
//public String(char[] bytes,int index,int length);把字符数组的一部分转换为字符串
String s5=new String(ch,2,3);
System.out.println("s5:"+s5);
System.out.println("s5.length():"+s5.length());
//public String(String orignial):把字符串常量值变成字符串
String s6=new String("abcde");
System.out.println("s6:"+s6);
System.out.println("s6.length():"+s6.length());
结果:
s1:
s1.length():0
s2:abcde
s2.length():5
s3:bcd
s3.length():3
s4:abcde
s4.length():5
s5:cde
s5.length():3
s6:abcde
s6.length():5
上一篇: 地图经纬度渲染插件和例子
下一篇: Hibernate框架简单使用——示例