Java String类

Posted H+

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 }

 


以上是关于Java String类的主要内容,如果未能解决你的问题,请参考以下文章

Failed to convert property value of type ‘java.lang.String‘ to required type ‘int‘ for property(代码片段

Jsp获取Java的重定向赋值(String)

solr分布式索引实战分片配置读取:工具类configUtil.java,读取配置代码片段,配置实例

java Ftp上传创建多层文件的代码片段

如何在片段类而不是活动类中使用底页?

java 代码片段