Java String类

一,String类

1、字符串是一个特殊的对象。
2、字符串一旦初始化就不可以被改变。

String类就代表一个字符串,不可以被复写,因为它没有子类。

Demo:

 1 package com.hpioneer.Demo;
 2 
 3 /**
 4  * @Description: String类
 5  * @Author: HPioneer
 6  * @CreateTime: 2018/5/7  11:13
 7  * @File: Java_String of JavaProject in com.hpioneer.Demo
 8  * @FullFileName: com.hpioneer.Demo.Java_String
 9  * @Create By IntelliJ
10  * @Version: 1.0
11  */
12 public class  Demo_String{
13     public static void main(String[] args) {
14 
15         //字符数组转字符串
16         char[] Array = { 'r', 'u', 'n', 'o', 'o', 'b'};
17         String string = new String(Array);
18         System.out.println( string );
19         System.out.println(string.length()); //长度
20         System.out.println("1、hello,".concat(string)); //拼接
21         System.out.println(2+ "、hello," + string); //拼接
22         System.out.printf("浮点型变量的值为 " +"%f, 整型变量的值为 " +" %d, 字符串变量的值为 " +"is %s" , 1.0, 1, "1.0");//格式化
23         System.out.println();
24 
25         String q = "a";
26         String w = "b";
27         String e = q + w;
28         String r = new StringBuffer().append(q).append(w).toString(); //字符串相加的实现就是Buffer进行append之后tostring
29 
30         String a = "saff";
31         String b = "saff";
32         String c = new String("saff");
33         System.out.println(a.equals(b));  // true
34         System.out.println(a.equals(c));  // true
35     }
36 
37 }

二,StringBuffer类

Demo:

 1 package com.hpioneer.Demo;
 2 
 3 /**
 4  * @Description:
 5  * @Author: HPioneer
 6  * @CreateTime: 2018/5/7  11:58
 7  * @File: Demo_StringBuffer of JavaProject in com.hpioneer.Demo
 8  * @FullFileName: com.hpioneer.Demo.Demo_StringBuffer
 9  * @Create By IntelliJ
10  * @Version: 1.0
11  */
12 public class Demo_StringBuffer {
13     public static void main(String[] args) {
14 
15        /* StringBuffer s  = new StringBuffer();
16         System.out.println(s.length());//0
17         System.out.println(s.capacity());//16 初始容量
18 
19         StringBuffer s1  = new StringBuffer(10);
20         System.out.println(s1.length());//0
21         System.out.println(s1.capacity());//10
22 
23         StringBuffer s2  = new StringBuffer("asas");
24         System.out.println(s2.length());//8
25         System.out.println(s2.capacity());//24 = 8+16
26 
27         System.out.println(s2.append(true));//asastrue
28         s2.deleteCharAt(0);
29         s2.delete(0,3);
30         System.out.println(s2.append("dfdfdf"));//truedfdfdf
31         System.out.println(s2.append(100));//truedfdfdf100
32         System.out.println(s2.insert(4,"KK"));//trueKKdfdfdf100
33         System.out.println(s2.reverse());//001fdfdfdKKeurt
34         System.out.println(s2.replace(0,2,"111"));//1111fdfdfdKKeurt
35         System.out.println(s2.substring(2,5));//11f*/
36 
37         /*StringBuffer sb = new StringBuffer("heima");
38 
39         String s1 = new String(sb);                        //通过构造将StringBuffer转换为String
40         System.out.println(s1);
41 
42         String s2 = sb.toString();                        //通过toString方法将StringBuffer转换为String
43         System.out.println(s2);
44 
45         String s3 = sb.substring(0, sb.length());        //通过截取子字符串将StringBuffer转换为String
46         System.out.println(s3);*/
47 
48         String s = "heima";
49         System.out.println(s);
50         change(s);
51         System.out.println(s);
52         //System.out.println(s+="itca");
53         System.out.println("---------------------");
54         StringBuffer sb = new StringBuffer();
55         sb.append("heima");
56         System.out.println(sb);
57         change(sb);
58         System.out.println(sb);
59 
60         
61     }
62 
63    public static void change(StringBuffer sb) {
64         sb.append("itcast");
65     }
66 
67     public static void change(String s) {
68         s += "itcast";
69     }
70     
71 }

猜你喜欢

转载自www.cnblogs.com/H---/p/9002318.html