任何信息,基本都是以文字的形式傳播和記錄下來的。
在計算機中,文字就是字符的集合,也就是字符串,C就是因為對字符串設計的不好,才那么容易溢出。而別的一些高級語言,對于這個進行了很多的改進。
編程的人由于技術方向和應用方向的不同,日常編程的內容差距很大。但是對于字符串的處理,那可是永遠都避不開的工作。
昨天跑步的時候,想了一下,對于字符串的操作有那么多(search,match,split,replace),感覺很煩雜,能不能抓住這些操作的一個基本集?
不知道對不對,反正想出來了一個,這個基本操作就是search,這里的search的意思是:在輸入串中找到目標串的開始位置(start index),和結束位置(end index)。
有了這個基本集,別的操作都很好衍生出來:
局部match:其實就是要求search操作至少返回一個start index。
全match:其實要求search操作的至少返回一個start index,并且start index要為零,end index要為輸入串的全長。
split:其實就是search操作之后,把前一個end index和當前的start index之間的字符串截出來而已。
replace:其實就是search操作之后,把start index和end index之間的字符串換成另外的而已。
所以,歸根到底,都是一個search操作的拓展罷了。這么一想,感覺清晰多了。
這么一來,API對search的能力支持的好壞和效率高低是衡量字符串操作功能的標準,當然,如果有直接支持match,split,replace操作的話就更好了。
java對字符串search的支持,最基本的就是下面的String的indexOf方法:
int indexOf(String str)
????????? Returns the index within this string of the first occurrence of the specified substring.
這里我想說的是,很多時候我們所謂要search的目標串,根本就不是固定單一的,而是變化多樣的。如果只有一兩種情況,最多用兩次上面的方法唄。但是有些情況是近乎不可能羅列的,例如,我們講的代表email的字符串,我們不可能遍歷它吧。
所以,需要一種能夠通用表達字符串格式的語言。這就是Regular Expression(re)。
假如上面方法indexOf的str參數能支持re做為參數的話,那對于這種多樣的search也可以用上面的方法了。
可惜,indexOf不支持re作為參數。
so,以下就介紹java api中可以用re作為參數的字符串操作方法(參數中的regex就是re)。
--------------------->>
String類的:
全match操作:
boolean matches(String regex)
????????? Tells whether or not this string matches the given regular expression.
全replace操作:
String replaceAll(String regex, String replacement)
????????? Replaces each substring of this string that matches the given regular expression with the given replacement.
首個replace操作:
String replaceFirst(String regex, String replacement)
????????? Replaces the first substring of this string that matches the given regular expression with the given replacement.
全split操作:
String[] split(String regex)
????????? Splits this string around matches of the given regular expression.
有限制數的split操作:
String[] split(String regex, int limit)
????????? Splits this string around matches of the given regular expression.
<<---------------------
可惜啊,可惜,可惜java的String類里面沒有可以支持re的search方法,那如果要用re來search,只好使用java中專門的re類庫。
java中的re類庫主要就兩個類,一個叫Pattern,顧名思義,代表re的類。一個叫Matcher類,反映當前match狀況的類(如存放了當前search到的位置,匹配的字符串等等信息)。
一般在構造中,“re的表達式”作為參數傳遞入Pattern類,“輸入串(待過濾串)”作為參數傳遞入Matcher類。
然后使用Matcher類的字符串search方法就可以了。Matcher真正提供search功能的API叫find。下面列出。
--------------------->>
Matcher類search操作相關的方法:
boolean lookingAt()
????????? Attempts to match the input sequence, starting at the beginning, against the pattern.
boolean matches()
????????? Attempts to match the entire input sequence against the pattern.
boolean find()
????????? Attempts to find the next subsequence of the input sequence that matches the pattern.
String group()
????????? Returns the input subsequence matched by the previous match.
<<---------------------
前三個都是search方法,返回成功與否。第四個是返回當前search上的字符串。
ok,至此。使用re的search操作也有眉目了。
當然,Pattern和Matcher也包含直接使用re進行的match,split,replace操作。
--------------------->>
Patter類別的字符串操作方法
全match操作:
static boolean matches(String regex, CharSequence input)
????????? Compiles the given regular expression and attempts to match the given input against it.
全split操作:
String[] split(CharSequence input)
????????? Splits the given input sequence around matches of this pattern.
有限制數的split操作:
String[] split(CharSequence input, int limit)
????????? Splits the given input sequence around matches of this pattern.
Matcher類別的字符串操作方法
全replace操作:
String replaceAll(String replacement)
????????? Replaces every subsequence of the input sequence that matches the pattern with the given replacement string.
首個replace操作:
String replaceFirst(String replacement)
????????? Replaces the first subsequence of the input sequence that matches the pattern with the given replacement string.
動態replace(replacement可以根據被替代的字符串變化而變化)
Matcher appendReplacement(StringBuffer sb, String replacement)
????????? Implements a non-terminal append-and-replace step.
StringBuffer appendTail(StringBuffer sb)
????????? Implements a terminal append-and-replace step.
<<---------------------
總結:
當必須使用re的時候,search操作就要用到Pattern,Matcher,當然動態的replace操作也要用到這兩個類。而別的match,replace,split操作,可以使用pattern,Matcher,當然也可以直接使用String,推薦還是用回咱們的String吧。
注:以上都是看jdk1.4以上的文檔得出的結論,以前版本不能用不負責任。