<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)

    有趣網絡

    搜索

    積分與排名

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 日本片免费观看一区二区| 四虎成年永久免费网站| 四虎影视永久免费观看网址 | 国产成A人亚洲精V品无码性色 | 国产禁女女网站免费看| 亚洲欧好州第一的日产suv| 色妞WWW精品免费视频| 亚洲一卡2卡3卡4卡5卡6卡| 最近2019中文字幕免费看最新| 亚洲一级免费毛片| 成人A级毛片免费观看AV网站| 亚洲另类无码专区丝袜| 国产美女无遮挡免费网站| 99亚洲乱人伦aⅴ精品| 免费h成人黄漫画嘿咻破解版| 一级a性色生活片久久无少妇一级婬片免费放| 国产在线观看免费不卡| 在线免费视频你懂的| 亚洲激情在线视频| 在线a级毛片免费视频| 亚洲精品宾馆在线精品酒店| 亚洲国产成人乱码精品女人久久久不卡 | 日本免费在线观看| 久久久久亚洲AV无码观看| 无码国产精品久久一区免费 | 亚洲乱妇熟女爽到高潮的片| 亚洲日韩国产一区二区三区| 久久精品国产影库免费看| 亚洲色图黄色小说| 日本久久久免费高清| 中国人免费观看高清在线观看二区| 久久亚洲国产精品五月天| 亚洲第一成年免费网站| 羞羞视频网站免费入口| 婷婷精品国产亚洲AV麻豆不片 | 中文字幕无线码中文字幕免费| 亚洲AV日韩AV天堂久久| 成人免费视频一区| 你懂的在线免费观看| 亚洲一区中文字幕| 久久亚洲中文字幕精品一区四|