標籤:
一、String
關於String類型變數,我們必須知道String並不是基礎資料型別 (Elementary Data Type),而是一個對象,String對象為不可變對象,一旦在記憶體中建立,內容不能發生變化,若要對字串內容改變,那麼就會建立新對象。
當建立字串對象的時候,我們知道在初始化之前,值為null。
String s1 = ””;String s2 = null;String s3 = new String();
s2表示的對象是還未建立,因此,它的記憶體尚未分配
s1,s3則表示有記憶體空間,只是空間裡面沒有值
String常用方法:
equals() ——判斷內容是否相同。
compareTo() ——判斷字串的大小關係。
compareToIgnoreCase(String int) ——在比較時忽略字母大小寫。
equalsIgnoreCase() ——忽略大小寫情況下判斷內容是否相同。
reagionMatches() ——對字串中的部分內容是否相同進行比較
charAt(int index) ——返回指定索引index位置上的字元,索引範圍從0開始。
indexOf(String str)——從字串開始檢索str,並返回第一次出現的位置,未出現返回-1。
indexOf(String str,int fromIndex);——從字串的第fromIndex個字元開始檢索str。
lastIndexOf(String str)——尋找最後一次出現的位置。
lastIndexOf(String str,int fromIndex)—-從字串的第fromIndex個字元尋找最後一次出現的位置。
starWith(String prefix,int toffset)—–測試此字串從指定索引開始的子字串是否以指定首碼開始。
tarWith(String prefix)——測試此字串是否以指定的首碼開始。
endsWith(String suffix)——測試此字串是否以指定的尾碼結束。
public String subString(int beginIndex)——返回一個新的字串,它是此字串的一個子字串。
public String subString(int beginIndex,int endIndex)——返回的字串是從beginIndex開始到endIndex-1的串。
public String replace(char oldChar,char newChar)。
public String replace(CharSequence target,CharSequence replacement)——把原來的etarget子序列替換為replacement序列,返回新串。
public String replaceAll(String regex,String replacement)——用Regex實現對字串的匹配。
二、StringBuilder/StringBuffer
StringBuffer是安全執行緒的,同步處理的,效能稍慢
StringBuilder是非安全執行緒的,並發處理的,效能稍快(建議使用)
三、String測試
/** * Java中的字串對象為不變對象,意思是字串對象建立後,內容不能改變,若想改變內容必須建立新對象。 * java使用字串常量池來緩衝通過字面量建立的字串對象,並重用它們。 * @author Administrator * */public class StringDemo1 { public static void main(String[] args) { String s1 = "Hello"; String s2 = "World"; String s3 = s1 + s2; String s4 = "HelloWorld"; String s5 = "Hello"; System.out.println(s1==s5); System.out.println(s3==s4); System.out.println(s3.equals(s4)); s4 += "!"; System.out.println(s3==s4); }}
public class StringDemo2 { public static void main(String[] args) { /* * 編譯器有一個最佳化措施,在編譯來源程式時,所計算表達是兩邊的內容都是字面量 * 那麼會直接計算,並將結果編譯到位元組碼檔案中 * 這樣的好處在於JVM運行時不用每次都計算了。 * 所以下面的代碼在位元組碼檔案中為 * String = "123abc" */ String s1 = "123abc"; String s2 = "123abc"; String s3 = "123" + "abc"; String s4 = "123"; String s5 = s4 + "abc"; String s6 = 1 + "23" + "abc"; String s7 = 1 + ‘2‘ + "3abc";//53abc String s8 = "33abc"; String s9 = new String("123abc"); System.out.println(s1==s9); System.out.println(s7); System.out.println(s1==s3); System.out.println(s1==s5); System.out.println(s1==s6); System.out.println(s1==s7); }}
public class StringDemo3 { /** * int length() * 返回當前字串的字元數量 */ @Test public void testLength(){ String s = "HelloWorld"; int len = s.length(); System.out.println(len); } @Test public void testIndexOf(){ String s = "HelloWorld"; /* * int indexOf(String str) * 查看給定的字串在當前字串中的位置,若有,則返回給定字串第一個字元在當前字串中的下標位置。 * 若沒有,則返回 -1. * */ int index = s.indexOf("l"); System.out.println("index:" + index); index = s.indexOf("L"); System.out.println("index:" + index); /* * 重載方法:可以從給定位置開始尋找,第一次數顯給定字串的位置 */ index = s.indexOf("l", 3); System.out.println("index:" + index); /* * int lastIndexOf(String str) * 查看給定字串在當前字串中最後一次出現的位置 */ index = s.lastIndexOf("l"); System.out.println("lastIndex:" + index); } @Test public void testSubString(){ /* * String substring(int start,int end) * 截取字串 * * java api 中通常使用2個數字表示範圍的時候,都是含頭不含尾的。 * 下面的:包含下標為5對應的字元,但是不包含8對應的字元 */ String s = "HelloWorld"; String sub = s.substring(5, 8);//前閉後開,包括5,不包括8的新字串 System.out.println(sub); /* * 重載方法: * String substring(int start) * 從給定位置開始一直截取到字串末尾 */ sub = s.substring(4); System.out.println(sub); } /* * 擷取網域名稱 * 從第一個 "." 之後的第一個字元開始截取到 第二個 "." 之前的字串。 */ @Test public void testYuMing(){ String url = "www.baidu.com/?video.video"; int start = url.indexOf(".") + 1;//第一次位置// int end = url.lastIndexOf("."); int end = url.indexOf(".", start);//第二次位置 String yuming = url.substring(start, end); System.out.println(yuming); } @Test public void testTrim(){ String str = " Hello World "; /* * String trim() * 去除當前字串兩邊的空白 */ String trim = str.trim(); System.out.println(str); System.out.println(trim); } @Test public void testCharAt(){ /* * char charAt(int index) * 查看當前字串中給定位置的字元 */ String str = "HelloWorld"; char charAt = str.charAt(5);//查看下標為5的字元是 ?即第六個字元 System.out.println(charAt); } @Test public void startsAndEndsWidth(){ /* * boolean startsWith(String str) * 判斷當前字串是否是以給定的字串起始的 * * boolean endsWith(String str) * 判斷當前字串是否是以給定的字串結尾的 */ String str = "Hello World"; boolean bool = str.startsWith("Hello"); System.out.println("以Hello開頭:" + bool); bool = str.startsWith("he"); System.out.println(bool); bool = str.endsWith("World"); System.out.println("以World開頭:" + bool); bool = str.endsWith("LD"); System.out.println(bool); } @Test public void testToUpperCaseAndToLowerCase(){ /* * String toLowerCase() * 將當前字串中的英文部分轉換為全小寫 * * String toUpperCase() * 將當前字串中的英文部分轉換為全大寫 */ String str = "你好 HelloWorld"; String toLower = str.toLowerCase(); System.out.println(toLower); String toUpper = str.toUpperCase(); System.out.println(toUpper); /* * 該方法常被用來忽略大小寫字串驗證上 */ } @Test public void testValueOf(){ /* * static String valueOf() * 該方法用若干重載,用於將其他類型轉換為字串 */ int i = 123; String iStr = String.valueOf(i); iStr += 4; System.out.println(iStr); double d = 123.456; String dStr = String.valueOf(d); System.out.println(dStr); System.out.println(String.valueOf(true)); char[] c = new char[]{‘a‘,‘b‘,‘c‘,‘d‘,‘e‘}; String cStr = String.valueOf(c); System.out.println(cStr); } }
四、StringBuilder/StringBuffer測試
/** * StringBuilder * 其內部維護一個可變的字元數組,該類用於修改字串使用 * @author Administrator * */public class StringBuilderDemo { @Test public void testToString() { StringBuilder sb = new StringBuilder(); StringBuilder sb1 = new StringBuilder("HelloWorld"); //建立一個StringBuilder來修改字串 StringBuilder strBu = new StringBuilder("學習Java"); String str = strBu.toString();//通過調用toString方法來擷取內部表示的字串 System.out.println(str); } @Test public void testStringBuilderMothod(){ /* * StringBuilder append(String str) * 向當前字串末尾追加給定字串的內容 * 傳回值就是當前StringBuilder對象,就是this * 這樣做是為了連續編輯操作 */ StringBuilder sb = new StringBuilder("學習java"); System.out.println(sb.toString()); sb.append("贏取。。。");// sb.append(""); System.out.println(sb.toString()); sb.replace(8, sb.length(), "改變世界");//替換 System.out.println(sb.toString()); sb.delete(0, 8);//刪除哪個區間的字串 System.out.println(sb.toString()); sb.insert(0, "活著");//在哪裡插入什麼字串 System.out.println(sb.toString()); sb.reverse();//反轉字串 System.out.println(sb.toString()); }}
Java學習之String