1.String類:String 是不可變字符序列數組
1) char charAt(int index)返回字符串中第 index 個字符。app
2) boolean equalsIgnoreCase(String other) 若是字符串與other
相等(忽略大小寫),則返回 true ide
3) int indexOf(String str) lastIndexOf() 查找指定字符串
出現的位置(下標)spa
4) int length()返回字符串的長度。code
5) String replace(char oldChar,char newChar)返回一個新串,
它 是 經過 用 newChar 替 換 此字 符 串中 出 現的 所 有
oldChar 而生成的對象
6) boolean startsWith(String prefix)若是字符串以 prefix 開始,
則返回 trueblog
7) boolean endsWith(String prefix) 若是字符串以 prefix 結
尾,則返回 true內存
8) String substring(int beginIndex)字符串
String substring(int beginIndex,int endIndex)返回一個新字符
串,該串包含從原始字符串 beginIndex 到串尾或 endIndex-1
的全部字符源碼
9) String toLowerCase()返回一個新字符串,該串將原始字符
串中的全部大寫字母改爲小寫字母
10) String toUpperCase()返回一個新字符串,該串將原始字
符串中的全部小寫字母改爲大寫字母
11) String trim() 返回一個新字符串,該串刪除了原始字
符串頭部和尾部的空格
public class MyString { public static void main(String[] args) { String str1 = "hello word"; String str2 = "hello word"; System.out.println(str1.charAt(1));//e 返回字符串中第 index 個字符。 System.out.println(str1.length());//10 System.out.println(str1.equalsIgnoreCase("abc"));//false System.out.println(str1.equals(str2));//true System.out.println(str1.indexOf('d'));//9 下標從0開始 System.out.println(str1.lastIndexOf('o'));//7 System.out.println(str1.replace('o', 'q'));//hellq wqrd System.out.println(str1);//hello word 上一條語句替換沒有發生在原來的字串中 而是產生一個新串做爲輸出 System.out.println(str1.startsWith("he"));//true System.out.println(str1.endsWith("rd"));//true System.out.println(str1.substring(3));//lo word System.out.println(str1.substring(1, 6));//ello System.out.println(str1);//hello word str1 = str1.substring(3); System.out.println(str1);//lo word String str3 = " HELlo word "; System.out.println(str3.trim());//HELlo word System.out.println(str3.toLowerCase());// hello word System.out.println(str3.toUpperCase());// HELLO WORD } }
2.String比較和構造方法
compareTo源碼:先比較每個字符是否相同,再比較長度
public int compareTo(String anotherString) { int len1 = value.length; int len2 = anotherString.value.length; int lim = Math.min(len1, len2); char v1[] = value; char v2[] = anotherString.value; int k = 0; while (k < lim) { char c1 = v1[k]; char c2 = v2[k]; if (c1 != c2) { return c1 - c2; } k++; } return len1 - len2; }
實例:
public class TestString { public static void main(String[] args) { String str="apple"; String str2="banana"; String str3="cat"; String str4="dog"; String str5="app"; /**求兩個字符串的長度的最小值,使用循環,比較對應位置上的字母,若是不相同,則對應位置字母的ASCII值相減*/ System.out.println(str.compareTo(str2)); //-1 97-98? System.out.println(str.compareTo(str3));//-2 97-99? System.out.println(str.compareTo(str4));//-3 97-100? //兩個字符串的長度相差 5-3=2 System.out.println(str.compareTo(str5)); //2 /**String類的構造方法*/ String s1=new String();//建立了一個長度爲0的字符串 String s2=null;//沒有建立對象,只是聲明瞭String類型的對象s2 String s3=new String("hello"); //經過char類型的數組構造String對象 char [] c={'a','b','c'}; String s4=new String(c); System.out.println(s4);//abc byte [] b={97,98,99}; String s5=new String(b); String s6=new String(c,0,2); System.out.println("s6:"+s6);//s6:ab } }
3.字符串相等的判斷
==:比較兩個字符串內存地址是否相等
equals(Object obj):用於比較兩個字符串對象的內容是否相等
1 public class TestString { 2 public static void main(String[] args) { 3 String str="abc"; 4 String str2=new String("abc"); 5 System.out.println("兩個對象的內存地址是否相等:"+(str==str2));//false 6 /**比較對應位置上的字符是否相等,若是全部的對象位置上的字符均相等,true,不然返回false*/ 7 System.out.println("兩個對象的內容是否相等:"+(str.equals(str2))); 8 //如下結果爲 false 9 /**由於Person對象 instanceof String 的結果爲false ,直接返回false*/ 10 System.out.println("String對象與Person對象的內容是否相等:"+(str.equals(new Person()))); 11 } 12 } 13 class Person{ 14 15 }
4.常量池:用於存儲在編譯期肯定,並被保存在已編譯的字節碼文件中的一些數據,包括 final 修飾變量及字符串變量,和符號引用量。
1 public class TestString2 { 2 public static void main(String[] args) { 3 String str1="abc"; 4 String str2="a"+"b"+"c"; 5 String str3=new String("abc"); 6 String str4=str3+""; 7 String str5=new String("abc"); 8 System.out.println("str1==str2:"+(str1==str2));//true 9 System.out.println("str1==str3:"+(str1==str3));//false 10 System.out.println("str1==str4:"+(str1==str4));//false 11 System.out.println("str3==str5:"+(str3==str5));//false 12 13 } 14 }
等號右側有new則在堆裏開空間