Java學習筆記—第九章 字串String

來源:互聯網
上載者:User

標籤:java學習   equals   字串   hello   world   

第九章 字串String

  1. Java中使用String類來建立一個字串變數,字串變數是類類型變數,是一個對象。聲明字串的文法格式如下:String s;

  2. 建立字串:通過String類提供的構造方法可建立字串,有以下幾種方式:

    (1)建立字串對象時直接賦值,例如:

     String s1 = "hello";

     String s2 = new String("hello");

    (2)由一個字串建立另一個字串,例如:

     String s1 = "hello";

     String s2 = new String(s1);

    (3)由字元型數組建立字串,例如:

     char[] c = {‘a‘,‘b‘,‘c‘};

     String s = new String(c);

  3. 擷取字串的長度:使用length()方法可以擷取一個字串的長度。

  4. 字串的串連:除了"+"運算子,還有concat()方法,例如:

    String s1 = "hello";

    String s2 = "world";

    String s3 = s1.concat(s2);

  5. 字串的比較:

    (1)equals方法:比較當前字串對象的內容與參數指定的字串對象的內容是否形同。例如:

    String s1 = new String("hello");

    String s2 = new String("hello");

    s1.equals(2);                    //結果為true

    比較字串是否相等不能用“==”,當用“==”比較兩個對象時,實際上是判斷兩個字串是否為同一個對象。因此,即使字串內容相同,由於是不同對象(對應引用地址不同),傳回值也為false,即運算式“s1==s2”的結果為false。

  6. euqalsIgnoreCase方法:忽略大小寫比較兩個字串的內容是否相同。例如:

    String s1 = new String("hello");

    String s2 = new String("Hello");

    s1.equalsIgnoreCase(s2);          //結果為true

  7. compareTo方法:按照字典順序比較兩個字元的大小。例如:

    String s = new String("abcde");

    s.compareTo("boy");     //小於0

    s.compareTo("aba")      //大於0

    s.compareTo("abcde")    //等於0

  8. 字串的檢索:

    (1)indexOf(String s):從當前字串的頭開始檢索字串s,返回首次出現s的位置。如果沒有則放回-1;(指定參數字串s也可以是指定字元char a)

    (2)lastIndexOf(String s):檢索當前字串,返回最後出現s的位置,如果沒有則返回-1。

    例如:

    String str = new String("I am a good cat");

    str.indexOf("a");      //傳回值為2

    str.lastIndexOf("a");  //傳回值為13

  9. 字串的截取:

    (1)substring(int index):從當前字串的index位置開始截取到最後得到一個子字串。

    (2)substring(int start,int end):從當前字串的start(包括start)位置開始,截取到end(不包括end)位置得到一個子字串。

    例如:

    String s1 = "helloworld";

    String s2 = s1.substring(2);   //s2為lloworld

    String s3 = s1.substring(3,5); //s3為lo

  10. 字串大小寫轉換:

    (1)toLowerCase():將當前字串的全部字元轉換為小寫。

    (2)toUpperCase():將當前字串的全部字元轉換為大寫。

    例如:

    String s = new String("Hello");

    s.toLowerCase();    //hello

    s.toUpperCase();    //HELLO

  11. 字元的替換:

    (1)replace(char old, char new):將當前字串由old指定的字元替換成由new指定的字元。

    (2)replaceAll(String old, String new):將當前字串中由old指定的字串替換成由new指定的字串。

    (3)trim():去掉當前字串首尾的空格

    例如:

    String s= "I mist sheep";

    String temp = s.replace(‘t‘,‘s‘);   //temp為I miss sheep

    String s1 = " I am a student ";

    String temp1 = s1.trim();           //temp1為I am a student

  12. 字串轉換為數值:通過調用Integer類的類方法parseInt(String s),可以將數字格式的字串,如“1234”轉化為int型資料。例如:

    String s = "1234";

    int x = Integer.parseInt(s);    //x=1234

    類似的其他資料類型也有相應的類方法將字串轉化由相應的基本類型。如parseByte()、parseShort()、parseDouble()、parseLong()。

  13. 數值轉換為字串:使用String類的valueOf方法可將數值轉換為字串。例如:

    String s = String.valueOf(789.456);   //s為"789.456"

  14. StringBuffer類:用於建立和操作動態字串,為該類對象分配的記憶體會自動擴充以容納新增的文本。適用於處理可變字串。

    (1)append()方法:將給定的字元追加到當前字串的末尾。

    (2)insert()方法:在當前字串的指定位置插入字元。

    無論是append或者insert,都是在原有對象基礎上操作,並沒有建立新的對象。

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.