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)

          有趣網絡

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 桂阳县| 清镇市| 兴业县| 磐石市| 高邮市| 嵊州市| 梓潼县| 日喀则市| 刚察县| 宾川县| 巴马| 临江市| 北海市| 宝山区| 宁明县| 东乌珠穆沁旗| 兴业县| 突泉县| 普兰店市| 延津县| 毕节市| 象州县| 桐柏县| 横峰县| 宁明县| 岳阳市| 海口市| 邹平县| 郸城县| 睢宁县| 札达县| 龙口市| 香港| 二手房| 湘乡市| 塘沽区| 百色市| 香港 | 扶沟县| 南雄市| 丰都县|