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