不用正則表達式替換字符串
Java語言: 臨時自用代碼@代碼發芽網
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 }
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