1 18 package org.objectweb.speedo.tools; 19 20 21 25 public class StringReplace { 26 public static String replaceChar(char oldChar, char newChar, String s) { 27 char[] cs = s.toCharArray(); 28 for (int i = 0; i < cs.length; i++) { 29 if (cs[i] == oldChar) { 30 cs[i] = newChar; 31 } 32 } 33 return new String (cs); 34 } 35 public static String replaceString(String old, String neo, String str) { 36 if (str == null || str.length() == 0) { 37 return str; 38 } 39 int i = 0; 40 int oldSize = old.length(); 41 int neoSize = neo.length(); 42 StringBuffer sb = new StringBuffer (str); 43 int begin = 0; 44 int idx = sb.indexOf(old, begin); 45 while(idx != -1) { 46 sb.delete(idx, idx + oldSize); 47 sb.insert(idx, neo); 48 begin = idx + neoSize; 49 if (begin < sb.length()) { 50 idx = sb.indexOf(old, begin); 51 } else { 52 idx = -1; 53 } 54 } 55 56 return sb.toString(); 57 } 58 59 public static String toJavaPattern(String str) { 60 str = StringReplace.replaceString("(", "\\(", str); 61 str = StringReplace.replaceString(")", "\\)", str); 62 str = StringReplace.replaceString(".", "\\.", str); 63 str = StringReplace.replaceString("*", ".*", str); 64 str = StringReplace.replaceString("?", ".?", str); 65 return str; 66 } 67 } 68 | Popular Tags |