Java String類

來源:互聯網
上載者:User

標籤:code   初始   對象   --   city   asa   初始化   子類   author   

一,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 IntelliJ10  * @Version: 1.011  */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之後tostring29 30         String a = "saff";31         String b = "saff";32         String c = new String("saff");33         System.out.println(a.equals(b));  // true34         System.out.println(a.equals(c));  // true35     }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 IntelliJ10  * @Version: 1.011  */12 public class Demo_StringBuffer {13     public static void main(String[] args) {14 15        /* StringBuffer s  = new StringBuffer();16         System.out.println(s.length());//017         System.out.println(s.capacity());//16 初始容量18 19         StringBuffer s1  = new StringBuffer(10);20         System.out.println(s1.length());//021         System.out.println(s1.capacity());//1022 23         StringBuffer s2  = new StringBuffer("asas");24         System.out.println(s2.length());//825         System.out.println(s2.capacity());//24 = 8+1626 27         System.out.println(s2.append(true));//asastrue28         s2.deleteCharAt(0);29         s2.delete(0,3);30         System.out.println(s2.append("dfdfdf"));//truedfdfdf31         System.out.println(s2.append(100));//truedfdfdf10032         System.out.println(s2.insert(4,"KK"));//trueKKdfdfdf10033         System.out.println(s2.reverse());//001fdfdfdKKeurt34         System.out.println(s2.replace(0,2,"111"));//1111fdfdfdKKeurt35         System.out.println(s2.substring(2,5));//11f*/36 37         /*StringBuffer sb = new StringBuffer("heima");38 39         String s1 = new String(sb);                        //通過構造將StringBuffer轉換為String40         System.out.println(s1);41 42         String s2 = sb.toString();                        //通過toString方法將StringBuffer轉換為String43         System.out.println(s2);44 45         String s3 = sb.substring(0, sb.length());        //通過截取子字串將StringBuffer轉換為String46         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類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.