JAVA中的String對象

來源:互聯網
上載者:User

標籤:http   返回   內容   .com   font   long   一個   位置   經典   

String 對象的特點
  1. String對象的資料不可改變!

  2. String類型對象封裝了一個字串數組

  3. 任何的操作也不能改變這個字元數組的內容

    String s = "123";String ss = s;s = s+"abc";System.out.println(s);//123abcSystem.out.println(ss);//123

說明: 在如上代碼中改變的是字串引用變數,但是字串沒有變!好處是字串可以“作為”基本類型使用!

原理:

字串常量的重用現象

Java中的字串常量是盡肯能重複使用的!好處是節省資源(記憶體)

  1. 字串字面量(直接量)在內容一樣時候重用同一個String對象。 String s1 = "123abc"; String s2 = "123abc"; //s1 s2 是字串類型的引用變數 //"123abc" 是直接量(字面量)

  2. 字串常量也參與重用!

  3. 字面量、常量的運算結果是字串,也重用同一個字串 String s4 = "123abc"; String s5 = 123 + "abc";//1+"23abc" System.out.println(s4==s5);//true

  4. 字串變數,變數的運算結果 和 新建立的字串對象不參與重用!! String name = in.nextLine();//Tom String s1 = "Tom and Jerry"; String s2 = name + " and Jerry"; System.out.println(s1==s2);//false String s3 = new String("Tom and Jerry"); System.out.println(s3==s1);//false

原理:

經典題目:

String s1 = "1"+"23"+"abc";String s2 = "1"+23+"abc";String s3 = '1'+23+"abc";System.out.print(s1==s2);System.out.print(s1==s3);如上代碼的執行結果:A.truetrue B.truefalse C.falsetrue D.falsefalse
字串中的字元

字串中封裝了一個字元數組,字串中的字元就是char類型的資料。

  1. char 類型是整數, 是一個字元的Unicode編碼。

  2. 16位不帶正負號的整數, 佔用2個位元組

案例:

String s = "Tom and Jerry";//          0123456789012char c = s.charAt(4);System.out.println(c);//aSystem.out.println((int)c);//97
indexOf 方法

找出一個字元在字串中的位置:

indexOf() 1. 如果有重複,找出左側第一個位置 2. 如果沒有找到,返回-1

案例:

String s = "Tom and Jerry";int i = s.indexOf('a'); System.out.println(i);//4i = s.indexOf('r');System.out.println(i);//10i = s.indexOf('X');System.out.println(i);//-1

str.indexOf("尋找字串", 起始位置)

String url = "http://tedu.cn/index.html";int i = url.indexOf("/",7);System.out.println(i);
lastIndexOf

反序尋找:從右至左尋找,返回字元的位置

String url = "http://tedu.cn/index.html";int i = url.lastIndexOf("/");//14System.out.println(i);//14
  1. 找到包 java.lang

  2. 找到類 String

  3. 找到方法 lastIndexOf()

substring 方法

從字串中截取一部分作為子字串

url.substring(起始位置)//從起始位置開始到最後截取為子字串String url = "http://tedu.cn/index.html";String filename = url.substring(15);// filename = index.htmlurl.substring(起始位置, 結束位置)//從起始位置開始到結束位置截取為子字串String url = "http://tedu.cn/index.html";//            01234567890123456// 包括起始不包括結束位置String str = url.substring(7, 14);String str = url.substring(7, 7+8);
trim
String str = " \t Tom \n \r";String s = str.trim();
startsWith endsWith

檢測一個字串是否以指定字串開頭或結尾

String str = "Hello World!";boolean b = str.startsWith("Hello");//trueb = str.startsWith("World");//falseb = str.endsWith("World");//falseb = str.endsWith("!");//true

案例:

String name = "demo.JPG";if(name.toLowerCase().endsWith(".jpg")){    System.out.println("圖片檔案");}
StringBuilder

Java 提供的用於計算字串的API, 其運算效能好:

案例:

String s = "A";s = s + "1";s = s + "1";s = s + "1";System.out.println(s);

原理:

效能比較:

    String s = "A";    long t1 = System.currentTimeMillis();    for(int i=0; i<10000; i++){        s = s+"1";    }    long t2 = System.currentTimeMillis();    System.out.println(s.length());    System.out.println(t2-t1);    StringBuilder ss =         new StringBuilder("A");    t1=System.nanoTime();    for(int i=0; i<10000; i++){        ss.append("1");    }    t2 = System.nanoTime();    System.out.println(ss.length());    System.out.println(t2-t1);


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.