<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    我的隱式生活(My Implicit Life)

    繼續搞“對象”,玩OO.

    首頁 新隨筆 聯系 聚合 管理
      11 Posts :: 1 Stories :: 39 Comments :: 0 Trackbacks

    任何信息,基本都是以文字的形式傳播和記錄下來的。

    在計算機中,文字就是字符的集合,也就是字符串,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以上的文檔得出的結論,以前版本不能用不負責任。

    posted on 2006-08-31 15:13 marco 閱讀(2692) 評論(0)  編輯  收藏 所屬分類: -=Java API=-
    主站蜘蛛池模板: 亚洲Aⅴ无码专区在线观看q| 亚洲人成网站18禁止一区| 亚洲精品无码久久久影院相关影片| 亚洲一区二区三区免费视频| 1000部国产成人免费视频| 亚洲一区精品中文字幕| 一区二区三区四区免费视频 | 成年女人18级毛片毛片免费| 亚洲欧洲日本精品| 1000部拍拍拍18免费网站| 亚洲国产精品成人综合色在线婷婷| 午夜精品免费在线观看| 亚洲一级二级三级不卡| 57pao国产成视频免费播放| 亚洲精品在线网站| 亚洲人成网站免费播放| 亚洲欧美第一成人网站7777 | 特级做A爰片毛片免费看无码 | 久久乐国产精品亚洲综合| a视频在线观看免费| 亚洲人成依人成综合网| 2021在线观看视频精品免费| 亚洲一区在线视频| 国产男女猛烈无遮档免费视频网站| 男女男精品网站免费观看| 亚洲色欲久久久综合网东京热| 亚洲视频在线免费观看| 国产日本亚洲一区二区三区| 国产精品免费播放| 免费无码又爽又刺激高潮软件| 亚洲最新永久在线观看| 免费做爰猛烈吃奶摸视频在线观看| 亚洲a∨无码精品色午夜| 国产午夜亚洲精品国产成人小说| 国产精品免费无遮挡无码永久视频| 亚洲在成人网在线看| 四虎永久精品免费观看| 免费成人在线视频观看| 亚洲人成人伊人成综合网无码| 亚洲午夜日韩高清一区| 97免费人妻无码视频|