懒得删 都发了
/*
* 字符串:由多个字符组成的一串数据。(是一个字符数组)
*
* 构造方法:
* String():空字符串
* String(byte[] bytes):把字节数组转成字符串
* String(byte[] bytes, int index, int count):把字节数组中的一部分转成字符串
* String(char[] value):把字符数组转成字符串
* String(char[] value, int index, int count):把字符数组中的一部分转成字符串
* String(String original):带内容的字符串
* 直接赋值使用:String s = "hello";
*
* 一个方法:
* public int length():返回字符串的长度
*/
public class StringDemo {
public static void main(String[] args) {
// String():空字符串
String s1 = new String();
System.out.println("s1:" + s1); // java.lang.String@12ab23 --
// 结果不是,说明重写了toString()方法.
System.out.println("s1.length():" + s1.length());
System.out.println("------------------------");
// 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());
System.out.println("------------------------");
// String(byte[] bytes, int index, int count):把字节数组中的一部分转成字符串
//String s3 = new String(bytes, 2, 2);
String s3 = new String(bytes, 0, 5);
System.out.println("s3:" + s3);
System.out.println("s3.length():" + s3.length());
System.out.println("------------------------");
//String(char[] value):把字符数组转成字符串
char[] chs = {'a','b','c','d','e'};
String s4 = new String(chs);
System.out.println("s4:"+s4);
System.out.println("s4.length():"+s4.length());
System.out.println("------------------------");
//String(char[] value, int index, int count):把字符数组中的一部分转成字符串
String s5 = new String(chs,1,3);
System.out.println("s5:"+s5);
System.out.println("s5.length():"+s5.length());
System.out.println("------------------------");
//String(String original):带内容的字符串
String s6 = new String("abcde");
System.out.println("s6:"+s6);
System.out.println("s6.length():"+s6.length());
System.out.println("------------------------");
//直接赋值使用:String s = "abcde";
String s7 = "abcde";
System.out.println("s7:"+s7);
System.out.println("s7.length():"+s7.length());
}
}
|