2008年5月15日星期四

不用正则表达式替换字符串

public class Test {
/**
* Simplest in Java 1.5, using the replace method, which
* takes CharSequence objects.
*/
public static String replace15(
    String aInput, String aOldPattern, String aNewPattern
){
    return aInput.replace(aOldPattern, aNewPattern);
}

/**
* Not quite as simple in Java 1.4. The replaceAll method works,
* but requires more care, since it uses regular expressions, which
* may contain special characters.
*/
public static String replace14(
    String aInput, String aOldPattern, String aNewPattern
){

    /*
    * The replaceAll method is a bit dangerous to use.
    * The aOldPattern is converted into a regular expression.
    * Thus, if aOldPattern may contain characters which have
    * special meaning to regular expressions, then they must
    * be 'escaped' before being passed to replaceAll. It is
    * easy to forget to do this.
    *
    * In addition, aNewPattern treats '$' as special characters
    * as well: they refer to 'back references'.
    */
    return aInput.replaceAll(aOldPattern, aNewPattern);
    /*
    Here is an alternative implementation using Pattern and Matcher,
    which is preferred when the same pattern is used repeatedly
    final Pattern pattern = Pattern.compile( aOldPattern );
    final Matcher matcher = pattern.matcher( aInput );
    return matcher.replaceAll( aNewPattern );
    */
}

/**
* If Java 1.4 is unavailable, the following technique may be used.
*
* @param aInput is the original String which may contain substring aOldPattern
* @param aOldPattern is the non-empty substring which is to be replaced
* @param aNewPattern is the replacement for aOldPattern
*/
public static String replaceOld(
    final String aInput,
    final String aOldPattern,
    final String aNewPattern
){
     if ( aOldPattern.equals("") ) {
        throw new IllegalArgumentException("Old pattern must have content.");
     }

     final StringBuffer result = new StringBuffer();
     //startIdx and idxOld delimit various chunks of aInput; these
     //chunks always end where aOldPattern begins
     int startIdx = 0;
     int idxOld = 0;
     while ((idxOld = aInput.indexOf(aOldPattern, startIdx)) >= 0) {
       //grab a part of aInput which does not include aOldPattern
       result.append( aInput.substring(startIdx, idxOld) );
       //add aNewPattern to take place of aOldPattern
       result.append( aNewPattern );

       //reset the startIdx to just after the current match, to see
       //if there are any further matches
       startIdx = idxOld + aOldPattern.length();
     }
     //the final chunk will go to the end of aInput
     result.append( aInput.substring(startIdx) );
     return result.toString();
}

/** Example: update an ip address appearing in a link. */
public static void main (String[] aArguments) {
    String OLD_IP = "insert into LOAD_POLIINFO (IDCARD,POLISTAT,JOINDATE,LOADNO) values ('110102197906300508','13',to_date('null ','yyyy-mm-dd'),70990)";
log(replaceOld(OLD_IP,"to_date('null ','yyyy-mm-dd')","null"));
}
private static void log(String aMessage){
    System.out.println(aMessage);
}
}

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

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

没有评论:

发表评论