Static Public Member Functions | |
String | replace (String sourceString, String replace, String with) |
String | replaceCasePreserving (String sourceString, String replace, String with) |
Definition at line 33 of file Strings.java.
|
Replaces all occurrences of a String within another String.
Definition at line 45 of file Strings.java. 00046 { 00047 if (sourceString == null || replace == null || with == null 00048 || "".equals(replace)) 00049 { 00050 return sourceString; 00051 } 00052 00053 StringBuffer buf = new StringBuffer(sourceString.length()); 00054 int start = 0, end = 0; 00055 while ((end = sourceString.indexOf(replace, start)) != -1) 00056 { 00057 buf.append(sourceString.substring(start, end)).append(with); 00058 start = end + replace.length(); 00059 } 00060 buf.append(sourceString.substring(start)); 00061 return buf.toString(); 00062 }
|
|
Replaces all occurrences of a String within another String. The String to be replaced will be replaced ignoring cases, all other cases are preserved in the returned string
Definition at line 75 of file Strings.java. 00077 { 00078 if (sourceString == null || replace == null || with == null) 00079 { 00080 return sourceString; 00081 } 00082 String lower = sourceString.toLowerCase(); 00083 int shift = 0; 00084 int idx = lower.indexOf(replace); 00085 int length = replace.length(); 00086 StringBuffer resultString = new StringBuffer(sourceString); 00087 do 00088 { 00089 resultString = resultString.replace(idx + shift, idx + shift + length, 00090 with); 00091 shift += with.length() - length; 00092 idx = lower.indexOf(with, idx + length); 00093 } 00094 while (idx > 0); 00095 00096 return resultString.toString(); 00097 }
|