可定制替换规则的文本替换器

来源:互联网 发布:巴萨恒大世俱杯数据 编辑:程序博客网 时间:2024/05/01 15:54

可以根据需要改进:

1. 让其不仅仅可以改SIM卡名称,适用于普遍的替换。(考虑提取抽象类)

2. 使用Omission策略判断可更多地使用正则表达式。(比如在判断 “ sim ” 和 ”simple“上)

3. 有一些特殊例子符合Omission策略,而实际却不能被忽略,这种情况应当能处理。(考虑修改Omission接口,增加一个会被优先处理的函数)

4. 改进效率,目前在太多的地方使用了String。


建议先阅读 main(), 以便了解代码功能。

package com.xxx.modify;import java.util.ArrayList;import java.util.List;/** * To change the sim name to uim name. *  * @author wycfgq@21cn.com *  */public final class SimNameModifer {// please make sure the length of 'SIM_STR' equals the length of 'UIM_STR'private static final String[] SIM_STR = { "sim", "Sim", "SIM" };private static final String[] UIM_STR = { "uim", "Uim", "UIM" };/** * see main() in this class. */public static CharSequence change1(CharSequence c) {return change2(c);}// TODO optimize: may use StringBuiler/** * see main() in this class. */public static String change2(CharSequence c) {if (c == null)return null;ArrayList<Omission> omits = new ArrayList<Omission>();omits.add(omitWord);String str = c.toString();for (int i = 0; i < SIM_STR.length; i++) {str = replace(str, SIM_STR[i], UIM_STR[i], omits);}return str;}// to omit the word include s1.// (e. s1 = "sim", the "simple" will be omit)private static Omission omitWord = new Omission() {@Overridepublic boolean isOmit(String str, String s1, String s2, int from) {int i = str.indexOf(s1, from);if (i < 0)return false;int before = i - 1; // the index before the s1int after = i + s1.length();boolean haveBefore = before >= 0;boolean haveAfter = after < str.length();if (haveBefore && isLetter(str.charAt(before)))return true;if (haveAfter && isLetter(str.charAt(after)))return true;return false;}private boolean isLetter(Character c) {return c.toString().matches("[a-zA-Z]");}};// according to the 'omits' rule2, replace all s1 in str with s2.private static String replace(String str, String s1, String s2,List<Omission> omits) {// TODO check args and throwint i = -1;StringBuilder sb = new StringBuilder(str);NO_REPLACE: while ((i = sb.indexOf(s1, i + 1)) >= 0) {if (omits != null) {for (Omission o : omits) {if (o.isOmit(str, s1, s2, i)) {continue NO_REPLACE;}}}sb.replace(i, i + s1.length(), s2);}return sb.toString();}// all System.out.println(...equals(...)) are print "true"public static void main(String[] args) {String s1 = "uim card is Uim card is UIM card, not simple.";String s2 = "sim card is Sim card is SIM card, not simple.";String result = change2(s2);System.out.println(result);System.out.println(s1.equals(result));result = change2("dsim");System.out.println(result);System.out.println("dsim".equals(result));result = change2("simple");System.out.println(result);System.out.println("simple".equals(result));System.out.println("the uim card".equals(change1("the sim card")));System.out.println("uim".equals(change1("sim")));result = change2("sim卡");System.out.println(result);System.out.println("uim卡".equals(result));CharSequence csq1 = "the uim card";CharSequence csq2 = "the sim card";CharSequence re = change1(csq2);System.out.println(re);System.out.println(csq1.equals(re));System.out.println(null == change1(null));}}interface Omission {/** * @param str * @param s1 * @param s2 * @param from * @return false: if want the 'str' to replace the 's1'(after index 'from') *         with 's2'. */public boolean isOmit(String str, String s1, String s2, int from);}

原创粉丝点击