標籤:http 返回 內容 .com font long 一個 位置 經典
String 對象的特點
-
String對象的資料不可改變!
-
String類型對象封裝了一個字串數組
-
任何的操作也不能改變這個字元數組的內容
String s = "123";String ss = s;s = s+"abc";System.out.println(s);//123abcSystem.out.println(ss);//123
說明: 在如上代碼中改變的是字串引用變數,但是字串沒有變!好處是字串可以“作為”基本類型使用!
原理:
字串常量的重用現象
Java中的字串常量是盡肯能重複使用的!好處是節省資源(記憶體)
-
字串字面量(直接量)在內容一樣時候重用同一個String對象。 String s1 = "123abc"; String s2 = "123abc"; //s1 s2 是字串類型的引用變數 //"123abc" 是直接量(字面量)
-
字串常量也參與重用!
-
字面量、常量的運算結果是字串,也重用同一個字串 String s4 = "123abc"; String s5 = 123 + "abc";//1+"23abc" System.out.println(s4==s5);//true
-
字串變數,變數的運算結果 和 新建立的字串對象不參與重用!! 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類型的資料。
-
char 類型是整數, 是一個字元的Unicode編碼。
-
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
-
找到包 java.lang
-
找到類 String
-
找到方法 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對象