I know of two ways of replacing all occurrences of substring in a string.
The regex way (assuming "substring-to-be-replaced" doesn't include regex special chars):
String regex = "substring-to-be-replaced" + "+";
Pattern scriptPattern = Pattern.compile(regex);
Matcher matcher = scriptPattern.matcher(originalstring);
newstring = matcher.replaceAll("replacement-substring");
The String.replace() way:
newstring = originalstring.replace("substring-to-be-replaced", "replacement-substring");
Which of the two is more efficient (and why)?
Are there more efficient ways than the above described two?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…