JDK源碼閱讀--String

 

 

public final class String
implements java.io.Serializable, Comparable<String>, CharSequence

 String被final修飾,表示String類不能被繼承。
 String類實現了Serializable、Comparable、CharSequence接口。
 /** The value is used for character storage. 字符串是以字符數組的形式存儲的*/ private final char value[]; /** Cache the hash code for the string */ private int hash; // Default to 0

 

equals方法:
 1 /**  2  * 將此字符串與指定對象進行比較,若是內容相同,則斷定它們相同  3  *  4  * @param anObject 指定的對象  5  * The object to compare this {@code String} against  6  *  7  * @return 若是兩個字符串相等(只要字符串的內容相等就算相等),則返回true,不然返回false.  8  *  9  * @see #compareTo(String) str1.equals(anObject) str2表示指定的對象,str1表示要比較的字符串 10  * @see #equalsIgnoreCase(String) 11 */ 12 public boolean equals(Object anObject) { 13 //若是這兩個對象相等,直接返回true。由於==相等表示地址相同,字符串的內容也相同。 14 if (this == anObject) { 15 return true; 16  } 17 //若是anObject是字符串類型的 18 if (anObject instanceof String) { 19 //強轉爲String類型 20 String anotherString = (String) anObject; 21 //獲取要比較的字符串的長度 22 int n = value.length; 23 //若是要比較的字符串長度 和 被比較的對象的字符串長度相同,再進行繼續比較 24 if (n == anotherString.value.length) { 25 char v1[] = value;//將要比較的字符串轉成char[]數組 26 char v2[] = anotherString.value;//將被比較的對象轉成char[]數組 27 int i = 0; 28 //兩個數組中的索引相同的字符是否都相同,只要有一個不相同,就返回false 29 //char類型的數據進行比較的時候,會進行類型提高,提高爲int類型進行比較 30 while (n-- != 0) { 31 if (v1[i] != v2[i]) 32 return false; 33 i++; 34  } 35 return true; 36  } 37  } 38 return false; 39 }

 

在String中的equals方法中會先判斷兩個對象的內存地址是否相等,若是相等,那麼equals就return true,若是不相等,則會進一步判斷傳過來的對象是否是字符串類型的,若是是則將兩個字符串對象char數組,而後一個一個字符的比較,若是都相等,則返回true。


startsWith方法:
 1    /**  2  * Tests if the substring of this string beginning at the  3  * specified index starts with the specified prefix.  4  *  5  * 測試源字符串是否從索引toffset處開始以字符串prefix開始  6  * @param prefix 前綴.  7  * @param toffset 索引 where to begin looking in this string.  8  * @return {@code true} if the character sequence represented by the  9  * argument is a prefix of the substring of this object starting 10  * at index {@code toffset}; {@code false} otherwise. 11  * The result is {@code false} if {@code toffset} is 12  * negative or greater than the length of this 13  * {@code String} object; otherwise the result is the same 14  * as the result of the expression 15  * <pre> 16  * this.substring(toffset).startsWith(prefix) 17  * </pre> 18 */ 19 public boolean startsWith(String prefix, int toffset) { 20 char ta[] = value;//源字符串轉成字符數組 21 int to = toffset; 22 char pa[] = prefix.value;//某個字符串prefix轉成字符數組 23 int po = 0; 24 int pc = prefix.value.length;//某個字符串的長度 25 // 索引的值不能小於0 也不能大於(源字符串長度-某個字符串prefix的長度),就是爲了避免讓索引越界。 26 if ((toffset < 0) || (toffset > value.length - pc)) { 27 return false; 28  } 29 30 //將源字符串轉成的字符數組的索引在toffset處開始 與 某個字符串prefix轉成的字符數組在索引在0處開始比較 31 while (--pc >= 0) { 32 //若是字符有不相同的,直接返回false 33 if (ta[to++] != pa[po++]) { 34 return false; 35  } 36  } 37 return true; 38  } 39 40 /** 41  * 測試源字符串是否以某個字符串prefix開始 42  * 43  * @param prefix 某個字符串prefix. 44  * @return true:表示源字符串以prefix;false:表示源字符串不是以prefix開始; 45  * @since 1. 0 46 */ 47 public boolean startsWith(String prefix) { 48 return startsWith(prefix, 0); 49 }

 

endsWith方法:
 1  /**  2  * 測試源字符串是否以某個字符串suffix結尾  3  *  4  * @param suffix 後綴.  5  * @return true:表示源字符串以suffix結尾;false:表示源字符串不是以suffix結尾;  6 */  7 public boolean endsWith(String suffix) {  8 /**  9  * 本質是調用startsWith方法,方法中的第二個參數就是用於計算出應該從哪一個位置開始比較的索引 10 */ 11 return startsWith(suffix, value.length - suffix.value.length); 12 }


replace方法:
 1 /**  2  * Returns a string resulting from replacing all occurrences of  3  * {@code oldChar} in this string with {@code newChar}.  4  * <p>  5  * If the character {@code oldChar} does not occur in the  6  * character sequence represented by this {@code String} object,  7  * then a reference to this {@code String} object is returned.  8  * Otherwise, a {@code String} object is returned that  9  * represents a character sequence identical to the character sequence 10  * represented by this {@code String} object, except that every 11  * occurrence of {@code oldChar} is replaced by an occurrence 12  * of {@code newChar}. 13  * <p> 14  * Examples: 15  * <blockquote><pre> 16  * "mesquite in your cellar".replace('e', 'o') 17  * returns "mosquito in your collar" 18  * "the war of baronets".replace('r', 'y') 19  * returns "the way of bayonets" 20  * "sparring with a purple porpoise".replace('p', 't') 21  * returns "starring with a turtle tortoise" 22  * "JonL".replace('q', 'x') returns "JonL" (no change) 23  * </pre></blockquote> 24  * 25  * @param oldChar 要被替換的字符(源字符串中的要被替換的字符)(注意,會將源字符串中全部等於oldChar的字符替換成newChar字符) 26  * @param newChar 用來替換的新字符 27  * @return 返回替換後的字符串 a string derived from this string by replacing every 28  * occurrence of {@code oldChar} with {@code newChar}. 29 */ 30 public String replace(char oldChar, char newChar) { 31 //若是老字符 和 新字符不相等 32 if (oldChar != newChar) { 33 int len = value.length;//獲取源字符串的長度 34 int i = -1; 35 char[] val = value; //將源字符串用字符數組的形式表現 我稱它爲源字符數組 36 37 //找出要被替換的字符,獲取到索引i,源字符數組中第i個元素要替換成字符newChar. 注意,這裏只找第一個和oldChar相等的字符的索引。 38 while (++i < len) { 39 if (val[i] == oldChar) { 40 break; 41  } 42  } 43 44 //若是索引小於源字符串長度,也就是索引沒有越界 45 if (i < len) { 46 //把源字符數組中的數據在索引 [0~i-1] 範圍的複製到 一個新的字符數組中 47 char buf[] = new char[len]; 48 for (int j = 0; j < i; j++) { 49 buf[j] = val[j]; 50  } 51 //對索引在[i,length-1] 範圍的進行操做 52 while (i < len) { 53 //獲取索引i處的元素的值 54 char c = val[i]; 55 //判斷源字符串中第i個元素是否和oldChar相等,若是相等,就替換成newChar,不然仍是存源字符串在索引i處的元素 56 buf[i] = (c == oldChar) ? newChar : c; 57 i++; 58  } 59 return new String(buf, true);//將替換後的字符數組轉成字符串並返回 60  } 61  } 62 return this;//若是oldChar和newChar相同,就把源字符串原封不動的返回 63 }
相關文章
相關標籤/搜索