String详解

 1 /*
 2  * 字符串:就是由多个字符组成的一串数据。也可以看成是一个字符数组。
 3  * 通过查看API,我们可以知道
 4  *         A:字符串字面值"abc"也可以看成是一个字符串对象。
 5  *         B:字符串是常量,一旦被赋值,就不能被改变。
 6  * 
 7  * 构造方法:
 8  *         public String():空构造
 9  *        public String(byte[] bytes):把字节数组转成字符串
10  *        public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串
11  *        public String(char[] value):把字符数组转成字符串
12  *        public String(char[] value,int index,int count):把字符数组的一部分转成字符串
13  *        public String(String original):把字符串常量值转成字符串
14  *
15  * 字符串的方法:
16  *         public int length():返回此字符串的长度。
17  */
18 public class StringDemo {
19     public static void main(String[] args) {
20         // public String():空构造
21         String s1 = new String();
22         System.out.println("s1:" + s1);
23         System.out.println("s1.length():" + s1.length());
24         System.out.println("--------------------------");
25 
26         // public String(byte[] bytes):把字节数组转成字符串
27         byte[] bys = { 97, 98, 99, 100, 101 };
28         String s2 = new String(bys);
29         System.out.println("s2:" + s2);
30         System.out.println("s2.length():" + s2.length());
31         System.out.println("--------------------------");
32 
33         // public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串
34         // 我想得到字符串"bcd"
35         String s3 = new String(bys, 1, 3);
36         System.out.println("s3:" + s3);
37         System.out.println("s3.length():" + s3.length());
38         System.out.println("--------------------------");
39 
40         // public String(char[] value):把字符数组转成字符串
41         char[] chs = { 'a', 'b', 'c', 'd', 'e', '爱', '林', '亲' };
42         String s4 = new String(chs);
43         System.out.println("s4:" + s4);
44         System.out.println("s4.length():" + s4.length());
45         System.out.println("--------------------------");
46 
47         // public String(char[] value,int index,int count):把字符数组的一部分转成字符串
48         String s5 = new String(chs, 2, 4);
49         System.out.println("s5:" + s5);
50         System.out.println("s5.length():" + s5.length());
51         System.out.println("--------------------------");
52         
53         //public String(String original):把字符串常量值转成字符串
54         String s6 = new String("abcde");
55         System.out.println("s6:" + s6);
56         System.out.println("s6.length():" + s6.length());
57         System.out.println("--------------------------");
58         
59         //字符串字面值"abc"也可以看成是一个字符串对象。
60         String s7 = "abcde";
61         System.out.println("s7:"+s7);
62         System.out.println("s7.length():"+s7.length());
63     }
64 }

字符串的特点:一旦被赋值,就不能改变。

 1 /*
 2  * 字符串的特点:一旦被赋值,就不能改变。
 3  */
 4 public class StringDemo {
 5     public static void main(String[] args) {
 6         String s = "hello";
 7         s += "world";
 8         System.out.println("s:" + s); // helloworld
 9     }
10 }

=========================================

 1 /*
 2  * String s = new String(“hello”)和String s = “hello”;的区别?
 3  * 有。前者会创建2个对象,后者创建1个对象。
 4  * 
 5  * ==:比较引用类型比较的是地址值是否相同
 6  * equals:比较引用类型默认也是比较地址值是否相同,而String类重写了equals()方法,比较的是内容是否相同。
 7  */
 8 public class StringDemo2 {
 9     public static void main(String[] args) {
10         String s1 = new String("hello");
11         String s2 = "hello";
12 
13         System.out.println(s1 == s2);// false
14         System.out.println(s1.equals(s2));// true
15     }
16 }

=========================================

 看程序写结果

 1 /*
 2  * 看程序写结果
 3  */
 4 public class StringDemo3 {
 5     public static void main(String[] args) {
 6         String s1 = new String("hello");
 7         String s2 = new String("hello");
 8         System.out.println(s1 == s2);// false
 9         System.out.println(s1.equals(s2));// true
10 
11         String s3 = new String("hello");
12         String s4 = "hello";
13         System.out.println(s3 == s4);// false
14         System.out.println(s3.equals(s4));// true
15 
16         String s5 = "hello";
17         String s6 = "hello";
18         System.out.println(s5 == s6);// true
19         System.out.println(s5.equals(s6));// true
20     }
21 }

=========================================

* 看程序写结果
* 字符串如果是变量相加,先开空间,在拼接。
* 字符串如果是常量相加,是先加,然后在常量池找,如果有就直接返回,否则,就创建。

 1 /*
 2  * 看程序写结果
 3  * 字符串如果是变量相加,先开空间,在拼接。
 4  * 字符串如果是常量相加,是先加,然后在常量池找,如果有就直接返回,否则,就创建。
 5  */
 6 public class StringDemo4 {
 7     public static void main(String[] args) {
 8         String s1 = "hello";
 9         String s2 = "world";
10         String s3 = "helloworld";
11         System.out.println(s3 == s1 + s2);// false
12         System.out.println(s3.equals((s1 + s2)));// true
13 
14         System.out.println(s3 == "hello" + "world");// false 这个我们错了,应该是true
15         System.out.println(s3.equals("hello" + "world"));// true
16 
17         // 通过反编译看源码,我们知道这里已经做好了处理。
18         // System.out.println(s3 == "helloworld");
19         // System.out.println(s3.equals("helloworld"));
20     }
21 }

=========================================

* String类的判断功能:
* boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
* boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
* boolean contains(String str):判断大字符串中是否包含小字符串
* boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
* boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾
* boolean isEmpty():判断字符串是否为空。

 1  * 注意:
 2  *         字符串内容为空和字符串对象为空。
 3  *         String s = "";
 4  *         String s = null;
 5  */
 6 public class StringDemo {
 7     public static void main(String[] args) {
 8         // 创建字符串对象
 9         String s1 = "helloworld";
10         String s2 = "helloworld";
11         String s3 = "HelloWorld";
12 
13         // boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
14         System.out.println("equals:" + s1.equals(s2));
15         System.out.println("equals:" + s1.equals(s3));
16         System.out.println("-----------------------");
17 
18         // boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
19         System.out.println("equals:" + s1.equalsIgnoreCase(s2));
20         System.out.println("equals:" + s1.equalsIgnoreCase(s3));
21         System.out.println("-----------------------");
22 
23         // boolean contains(String str):判断大字符串中是否包含小字符串
24         System.out.println("contains:" + s1.contains("hello"));
25         System.out.println("contains:" + s1.contains("hw"));
26         System.out.println("-----------------------");
27 
28         // boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
29         System.out.println("startsWith:" + s1.startsWith("h"));
30         System.out.println("startsWith:" + s1.startsWith("hello"));
31         System.out.println("startsWith:" + s1.startsWith("world"));
32         System.out.println("-----------------------");
33 
34         // 练习:boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾这个自己玩
35 
36         // boolean isEmpty():判断字符串是否为空。
37         System.out.println("isEmpty:" + s1.isEmpty());
38 
39         String s4 = "";
40         String s5 = null;
41         System.out.println("isEmpty:" + s4.isEmpty());
42         // NullPointerException
43         // s5对象都不存在,所以不能调用方法,空指针异常
44         System.out.println("isEmpty:" + s5.isEmpty());
45     }
46 }
原文地址:https://www.cnblogs.com/jiangjianzhu/p/5784778.html