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

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

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

    Loading...

    java .net

    不用正則表達式替換字符串

    01 public class Test {
    02 /**
    03 * Simplest in Java 1.5, using the replace method, which
    04 * takes CharSequence objects.
    05 */
    06 public static String replace15(
    07     String aInput, String aOldPattern, String aNewPattern){
    08     return aInput.replace(aOldPattern, aNewPattern);
    09 }
    10 /**
    11 * Not quite as simple in Java 1.4. The replaceAll method works,
    12 * but requires more care, since it uses regular expressions, which
    13 * may contain special characters.
    14 */
    15 public static String replace14(
    16     String aInput, String aOldPattern, String aNewPattern){
    17     /*
    18     * The replaceAll method is a bit dangerous to use.
    19     * The aOldPattern is converted into a regular expression.
    20     * Thus, if aOldPattern may contain characters which have
    21     * special meaning to regular expressions, then they must
    22     * be 'escaped' before being passed to replaceAll. It is
    23     * easy to forget to do this.
    24     *
    25     * In addition, aNewPattern treats '$' as special characters
    26     * as well: they refer to 'back references'.
    27     */
    28     return aInput.replaceAll(aOldPattern, aNewPattern);
    29     /*
    30     Here is an alternative implementation using Pattern and Matcher,
    31     which is preferred when the same pattern is used repeatedly
    32     final Pattern pattern = Pattern.compile( aOldPattern );
    33     final Matcher matcher = pattern.matcher( aInput );
    34     return matcher.replaceAll( aNewPattern );
    35     */
    36 }
    37 /**
    38 * If Java 1.4 is unavailable, the following technique may be used.
    39 *
    40 * @param aInput is the original String which may contain substring aOldPattern
    41 * @param aOldPattern is the non-empty substring which is to be replaced
    42 * @param aNewPattern is the replacement for aOldPattern
    43 */
    44 public static String replaceOld(
    45     final String aInput,
    46     final String aOldPattern,
    47     final String aNewPattern){
    48      if ( aOldPattern.equals("") ) {
    49         throw new IllegalArgumentException("Old pattern must have content.");
    50      }
    51      final StringBuffer result = new StringBuffer();
    52      //startIdx and idxOld delimit various chunks of aInput; these
    53      //chunks always end where aOldPattern begins
    54      int startIdx = 0;
    55      int idxOld = 0;
    56      while ((idxOld = aInput.indexOf(aOldPattern, startIdx)) >= 0) {
    57        //grab a part of aInput which does not include aOldPattern
    58        result.append( aInput.substring(startIdx, idxOld) );
    59        //add aNewPattern to take place of aOldPattern
    60        result.append( aNewPattern );
    61        //reset the startIdx to just after the current match, to see
    62        //if there are any further matches
    63        startIdx = idxOld + aOldPattern.length();
    64      }
    65      //the final chunk will go to the end of aInput
    66      result.append( aInput.substring(startIdx) );
    67      return result.toString();
    68 }
    69 /** Example: update an ip address appearing in a link. */
    70 public static void main (String[] aArguments) {
    71     String OLD_IP = "insert into LOAD_POLIINFO (IDCARD,POLISTAT,JOINDATE,LOADNO) values ('110102197906300508','13',to_date('null ','yyyy-mm-dd'),70990)";
    72 log(replaceOld(OLD_IP,"to_date('null ','yyyy-mm-dd')","null"));
    73 }
    74 private static void log(String aMessage){
    75     System.out.println(aMessage);
    76 }
    77 }

    參考自:http://www.javapractices.com/topic/TopicAction.do?Id=80

                 http://biostar.blog.sohu.com/69732830.html

    posted on 2008-08-26 22:07 閱讀(617) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     

    公告

    希望有一天

    我能用鼠標雙擊我的錢包

    然后選中一張100元

    按住“ctrl+c”

    接著不停的“ctrl+v”

    嘻嘻~~~笑醒~~~



    導航

    <2008年8月>
    272829303112
    3456789
    10111213141516
    17181920212223
    24252627282930
    31123456

    統計

    常用鏈接

    留言簿(6)

    隨筆分類(102)

    隨筆檔案(398)

    文章分類

    文章檔案(10)

    有趣網絡

    搜索

    積分與排名

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 日韩高清在线免费观看| 亚洲视频免费一区| 国产午夜无码视频免费网站| 亚洲一区精彩视频| 久久天天躁狠狠躁夜夜免费观看| 亚洲激情校园春色| 久久久久久久免费视频| 亚洲色欲色欲www| 日本一道高清不卡免费| 18禁亚洲深夜福利人口| 成人亚洲网站www在线观看| 污污视频免费观看网站| 综合久久久久久中文字幕亚洲国产国产综合一区首 | 777亚洲精品乱码久久久久久| 国产一区二区免费视频| 久久精品亚洲一区二区三区浴池| 日本免费一区二区在线观看| 亚洲AV成人无码天堂| 免费毛片网站在线观看| 四虎永久在线精品免费一区二区| 中文字幕亚洲日本岛国片| 国产精品免费观看调教网| 亚洲乱码卡三乱码新区| 国产大片免费观看中文字幕| 国产精品1024在线永久免费| 亚洲国产成人片在线观看无码| 91精品免费观看| 亚洲暴爽av人人爽日日碰| 国产精品亚洲w码日韩中文| 久久久久久久久久国产精品免费| 亚洲人成在线免费观看| 国产成人精品123区免费视频| 中文字幕免费视频精品一| 亚洲成人午夜电影| 免费一看一级毛片| 无码国产精品一区二区免费vr| 日本亚洲精品色婷婷在线影院| 免费v片在线观看品善网| 中文字幕免费在线| 黄色一级毛片免费| 7777久久亚洲中文字幕蜜桃|