Java常用方法使用心得,split,replaceAll

來源:互聯網
上載者:User

 

【轉】Java常用方法使用心得

不斷完善中ing . . .

java.lang.String split

String的split方法是直接按照給定的字串對字串進行拆分,例如

java 代碼

  1. String value = "a,b,c,d,e";
  2. String[] names = value.split(",");
  3. for(int i=0,n=names.length;i
  4. System.out.print(names[i]);
  5. }

運行結果:

a b c d e

但是在做ip解析時發現出了問題,代碼如下:

java 代碼

  1. String value = "209.242.1.1";
  2. String[] names = value.split(".");
  3. for(int i=0,n=names.length;i
  4. System.out.print(names[i]+" ");
  5. }

理想的輸出結果應該是219 242 1 1,結果什麼都沒有輸出。

很奇怪哦。看一下split的方法簽名吧。

java 代碼

  1. public String[] split(String regex)

這裡的參數的名稱是 regex ,也就是 Regular Expression (Regex)。這個參數並不是一個簡單的分割用的字元,而是一個Regex:

java 代碼

  1. public String[] split(String regex, int limit) {
  2. return Pattern.compile(regex).split(this, limit);
  3. }

split 的實現直接調用的 Matcher 類的 split 的方法。“ . ”在Regex中有特殊的含義,因此我們使用的時候必須進行轉義。

修改代碼如下:

java 代碼

  1. String value = "209.242.1.1";
  2. String[] names = value.split("//.");
  3. for(int i=0,n=names.length;i
  4. System.out.print(names[i]+" ");
  5. }

Replace 方法

首先看看Replace方法的介紹

java 代碼

  1. String java.lang.String.replace(char oldChar, char newChar)
  2. Returns a new string resulting from replacing all occurrences of oldChar in
  3. this string with newChar.
  4. If the character oldChar does not occur in the character sequence represented by
  5. this String object, then a reference to this String object is returned. Otherwise,

  6. a new String object is created that represents a character sequence identical to

  7. the character sequence represented by this String object, except that every

  8. occurrence of oldChar is replaced by an occurrence of newChar.
  9. Examples:
  10. "mesquite in your cellar".replace('e', 'o')
  11. returns "mosquito in your collar"
  12. "the war of baronets".replace('r', 'y')
  13. returns "the way of bayonets"
  14. "sparring with a purple porpoise".replace('p', 't')
  15. returns "starring with a turtle tortoise"
  16. "JonL".replace('q', 'x') returns "JonL" (no change)
  17. Parameters:
  18. oldChar the old character.
  19. newChar the new character.
  20. Returns:
  21. a string derived from this string by replacing every occurrence of oldChar with newChar.

用法 :

java 代碼

  1. @Test
  2. public void testReplace(){
  3. String A = "aaa bCskd dkkAik kdaFe";
  4. System.out.println(A.replace('a', '_'));
  5. }

結果為:___ bCskd dkkAik kd_Fe

此方法用來替換char字元,對字串不能處理

A.replace('aaa', '=') 是錯誤的

但是 A.replace(“aaa”, “=”)卻是可以的

java 代碼

  1. @Test
  2. public void testReplace(){
  3. String A = "aaa bCskd dkkAik kaaaFe";
  4. System.out.println(A.replace("aaa", "="));
  5. }

結果為:= bCskd dkkAik k=Fe

可見 replace("","")與方法replaceAll("","")擁有差不多的功能

仔細看看差別

java 代碼

  1. String java.lang.String.replace(CharSequence target, CharSequence replacement)
  2. Replaces each substring of this string that matches the literal target sequence

  3. with the specified literal replacement sequence. The replacement proceeds from

  4. the beginning of the string to the end, for example, replacing "aa" with "b" in

  5. the string "aaa" will result in "ba" rather than "ab".
  6. Parameters:
  7. target The sequence of char values to be replaced
  8. replacement The replacement sequence of char values
  9. Returns:
  10. The resulting string
  11. Throws:
  12. NullPointerException if target or replacement is null.
  13. Since:
  14. 1.5

是1.5之後才有的功能哦

但是 replaceAll卻更強大

java 代碼

  1. @Test
  2. public void testReplace(){
  3. String A = "aaa bCskd dkkAik kaaaFe";
  4. System.out.println(A.replace(" ", ""));
  5. }

結果為aaabCskddkkAikkaaaFe

replaceAll可以將字串內部的空格去掉

但是用replace(' ','')方法卻不可以

replace(" ","")方法可以

另外一點

java 代碼

  1. @Test
  2. public void testReplace(){
  3. String A = "aaa bCskd dkkAik kaaaFe";
  4. System.out.println(A.replaceAll("[a-z]", "="));
  5. }

結果為:=== =C=== ===A== ====F=

replaceAll可以用Regex,強大啊

我們看看replaceAll的用法

java 代碼

  1. String java.lang.String.replaceAll(String regex, String replacement)
  2. Replaces each substring of this string that matches the given regular expression

  3. with the given replacement.
  4. An invocation of this method of the form str.replaceAll(regex, repl) yields exactly

  5. the same result as the expression
  6. java.util.regex.Pattern. compile(regex).java.util.regex.Pattern.matcher(java.lang.CharSequence) matcher(str). replaceAll(repl)
  7. See Also:
  8. java.util.regex.Pattern
  9. Parameters:
  10. regex the regular expression to which this string is to be matched
  11. Returns:
  12. The resulting String
  13. Throws:
  14. PatternSyntaxException if the regular expression's syntax is invalid
  15. Since:
  16. 1.4
  17. @spec
  18. JSR-51

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.