Skip to content
This repository has been archived by the owner on Apr 3, 2018. It is now read-only.

Commit

Permalink
LANG: replaceAll
Browse files Browse the repository at this point in the history
  • Loading branch information
bruno-medeiros committed Aug 7, 2015
1 parent 6ee423a commit 3355cec
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions plugin_tooling/src-util/melnorme/utilbox/misc/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,28 @@ public static int occurrenceCount(String string, char character) {
public static String replaceStr(String str, int repOffset, int repLen, String repStr) {
return str.substring(0, repOffset) + repStr + str.substring(repOffset + repLen, str.length());
}

/**
* @return replace all occurrences of given matchStr with repStr, in given string.
* Similar to {@link String#replaceAll(String, String)} but doesn't use regexps,
* therefore doesn't require any quoting.
*/
public static String replaceAll(String string, String matchStr, String repStr) {
StringBuilder sb = new StringBuilder();
int startIx = 0;
while(true) {
int matchIx = string.indexOf(matchStr, startIx);
if(matchIx == -1) {
sb.append(string, startIx, string.length());
break;
}

sb.append(string, startIx, matchIx);
sb.append(repStr);
startIx = matchIx + matchStr.length();
}
return sb.toString();
}

/** Replace str with strRep in the given strb StringBuilder, if str occurs.
* @return true if str occurs in strb. */
Expand Down

0 comments on commit 3355cec

Please sign in to comment.