本文系轉載,轉自:http://feigme.iteye.com/blog/147259
java.lang.String split
String的split方法是直接按照給定的字串對字串進行拆分,例如
String value = "a,b,c,d,e"; String[] names = value.split(","); for(int i=0,n=names.length;i System.out.print(names[i]); }
運行結果:
a b c d e
但是在做ip解析時發現出了問題,代碼如下:
String value = "209.242.1.1"; String[] names = value.split("."); for(int i=0,n=names.length;i System.out.print(names[i]+" "); }
理想的輸出結果應該是219 242 1 1,結果什麼都沒有輸出。
很奇怪哦。看一下split的方法簽名吧。
public String[] split(String regex)
這裡的參數的名稱是regex
,也就是 Regular Expression(Regex)。這個參數並不是一個簡單的分割用的字元,而是一個Regex:
public String[] split(String regex, int limit) { return Pattern.compile(regex).split(this, limit); }
split
的實現直接調用的 Matcher 類的 split 的方法。“.
”在Regex中有特殊的含義,因此我們使用的時候必須進行轉義。
修改代碼如下:
String value = "209.242.1.1"; String[] names = value.split("\\."); for(int i=0,n=names.length;i System.out.print(names[i]+" "); }
Replace 方法
首先看看Replace方法的介紹
String java.lang.String.replace(char oldChar, char newChar) Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar. If the character oldChar does not occur in the character sequence represented by this String object, then a reference to this String object is returned. Otherwise, a new String object is created that represents a character sequence identical to the character sequence represented by this String object, except that every occurrence of oldChar is replaced by an occurrence of newChar. Examples: "mesquite in your cellar".replace('e', 'o') returns "mosquito in your collar" "the war of baronets".replace('r', 'y') returns "the way of bayonets" "sparring with a purple porpoise".replace('p', 't') returns "starring with a turtle tortoise" "JonL".replace('q', 'x') returns "JonL" (no change) Parameters: oldChar the old character. newChar the new character. Returns: a string derived from this string by replacing every occurrence of oldChar with newChar.
用法 :
@Test public void testReplace(){ String A = "aaa bCskd dkkAik kdaFe"; System.out.println(A.replace('a', '_')); }
結果為:___ bCskd dkkAik kd_Fe
此方法用來替換char字元,對字串不能處理
A.replace('aaa', '=') 是錯誤的
但是 A.replace(“aaa”, “=”)卻是可以的
@Test public void testReplace(){ String A = "aaa bCskd dkkAik kaaaFe"; System.out.println(A.replace("aaa", "=")); }
結果為:= bCskd dkkAik k=Fe
可見 replace("","")與方法replaceAll("","")擁有差不多的功能
仔細看看差別
String java.lang.String.replace(CharSequence target, CharSequence replacement)Replaces each substring of this string that matches the literal target sequencewith the specified literal replacement sequence. The replacement proceeds fromthe beginning of the string to the end, for example, replacing "aa" with "b" inthe string "aaa" will result in "ba" rather than "ab".Parameters:target The sequence of char values to be replacedreplacement The replacement sequence of char valuesReturns:The resulting stringThrows:NullPointerException if target or replacement is null.Since:1.5
是1.5之後才有的功能哦
但是 replaceAll卻更強大
@Test public void testReplace(){ String A = "aaa bCskd dkkAik kaaaFe"; System.out.println(A.replace(" ", "")); }
結果為aaabCskddkkAikkaaaFe
replaceAll可以將字串內部的空格去掉
但是用replace(' ','')方法卻不可以
replace(" ","")方法可以
另外一點
@Test public void testReplace(){ String A = "aaa bCskd dkkAik kaaaFe"; System.out.println(A.replaceAll("[a-z]", "=")); }
結果為:=== =C=== ===A== ====F=
replaceAll可以用Regex,強大啊
我們看看replaceAll的用法java 代碼
String java.lang.String.replaceAll(String regex, String replacement) Replaces each substring of this string that matches the given regular expression with the given replacement. An invocation of this method of the form str.replaceAll(regex, repl) yields exactly the same result as the expression java.util.regex.Pattern. compile(regex).java.util.regex.Pattern.matcher(java.lang.CharSequence) matcher(str). replaceAll(repl) See Also: java.util.regex.Pattern Parameters: regex the regular expression to which this string is to be matched Returns: The resulting String Throws: PatternSyntaxException if the regular expression's syntax is invalid Since: 1.4 @spec JSR-51