MCPcopy Create free account
hub / github.com/0opslab/opslabJutil / replace

Method replace

src/main/java/com/opslab/helper/StringHelper.java:250–276  ·  view source on GitHub ↗

字符串替换 @param inString @param oldPattern @param newPattern @return

(String inString, String oldPattern, String newPattern)

Source from the content-addressed store, hash-verified

248 * @return
249 */
250 public static String replace(String inString, String oldPattern, String newPattern) {
251 if (hasLength(inString) && hasLength(oldPattern) && newPattern != null) {
252 int index = inString.indexOf(oldPattern);
253 if (index == -1) {
254 return inString;
255 } else {
256 int capacity = inString.length();
257 if (newPattern.length() > oldPattern.length()) {
258 capacity += 16;
259 }
260
261 StringBuilder sb = new StringBuilder(capacity);
262 int pos = 0;
263
264 for (int patLen = oldPattern.length(); index >= 0; index = inString.indexOf(oldPattern, pos)) {
265 sb.append(inString.substring(pos, index));
266 sb.append(newPattern);
267 pos = index + patLen;
268 }
269
270 sb.append(inString.substring(pos));
271 return sb.toString();
272 }
273 } else {
274 return inString;
275 }
276 }
277
278
279 /**

Callers 3

toURIMethod · 0.95
getClassNameByJarMethod · 0.80
replaceXSSMethod · 0.80

Calls 2

hasLengthMethod · 0.95
toStringMethod · 0.45

Tested by

no test coverage detected