1 24 25 package org.objectweb.cjdbc.common.util; 26 27 34 public class Strings 35 { 36 37 46 public static String replace(String sourceString, String replace, String with) 47 { 48 if (sourceString == null || replace == null || with == null 49 || "".equals(replace)) 50 { 51 return sourceString; 52 } 53 54 StringBuffer buf = new StringBuffer (sourceString.length()); 55 int start = 0, end = 0; 56 while ((end = sourceString.indexOf(replace, start)) != -1) 57 { 58 buf.append(sourceString.substring(start, end)).append(with); 59 start = end + replace.length(); 60 } 61 buf.append(sourceString.substring(start)); 62 return buf.toString(); 63 } 64 65 76 public static String replaceCasePreserving(String sourceString, 77 String replace, String with) 78 { 79 if (sourceString == null || replace == null || with == null) 80 { 81 return sourceString; 82 } 83 String lower = sourceString.toLowerCase(); 84 int shift = 0; 85 int idx = lower.indexOf(replace); 86 int length = replace.length(); 87 StringBuffer resultString = new StringBuffer (sourceString); 88 do 89 { 90 resultString = resultString.replace(idx + shift, idx + shift + length, 91 with); 92 shift += with.length() - length; 93 idx = lower.indexOf(replace, idx + length); 94 } 95 while (idx > 0); 96 97 return resultString.toString(); 98 } 99 100 } 101 | Popular Tags |