Java Utililty Methods String Substritute

List of utility methods to do String Substritute

Description

The list of methods to do String Substritute are organized into topic(s).

Method

Stringsubstitute(final String input, final String pattern, final String sub)
Replace all patterns in a String
StringBuilder ret = new StringBuilder(input.length());
int start = 0;
int index = -1;
final int length = pattern.length();
while ((index = input.indexOf(pattern, start)) >= start) {
    ret.append(input.substring(start, index));
    ret.append(sub);
    start = index + length;
...
Stringsubstitute(String in, String find, String newString)

substitute returns a string in which 'find' is substituted by 'newString'

if (in == null || find == null || newString == null)
    return in;
char[] working = in.toCharArray();
StringBuffer stringBuffer = new StringBuffer();
int startindex = in.indexOf(find);
if (startindex < 0)
    return in;
int currindex = 0;
...
Stringsubstitute(String original, String match, String subst)
Find all occurences of the "match" in original, and substitute the "subst" string.
String s = original;
int pos;
while (0 <= (pos = s.indexOf(match))) {
    StringBuilder sb = new StringBuilder(s);
    s = sb.replace(pos, pos + match.length(), subst).toString();
return s;
Stringsubstitute(String s, String from, String to)
substitute
if (s == null || from == null || s.equals("") || from.equals("")) {
    return s;
String res = "";
int ind = s.indexOf(from);
while (ind >= 0) {
    res += s.substring(0, ind);
    res += to;
...
Stringsubstitute(String str, CharSequence... substitutionSeqs)
substitute
StringBuilder buffer = new StringBuilder(str.length() + 32);
int index = 0;
while (index < str.length()) {
    int startIndex = index;
    index = str.indexOf(SUBSTITUTION_PLACEHOLDER_PREFIX_CHAR, index);
    if (index < 0)
        index = str.length();
    if (index > startIndex)
...
Stringsubstitute(String str, String from, String to)
substitute
if (from == null || from.equals("") || to == null)
    return str;
int index = str.indexOf(from);
if (index == -1)
    return str;
StringBuilder buf = new StringBuilder(str.length());
int lastIndex = 0;
while (index != -1) {
...
Stringsubstitute(String str, String source, String target)
substitute
int index = str.indexOf(source);
if (index == -1) 
    return str;
int start = index, end = index + source.length();
return str.substring(0, start) + target + str.substring(end);
Stringsubstitute(String str, String source, String target)
In a string, replace one substring with another.
int index = str.indexOf(source);
if (index == -1) 
    return str;
int start = index, end = index + source.length();
return str.substring(0, start) + target + str.substring(end);
Stringsubstitute(String str, String variable, String value, int num)
A utility method for substituting parts of Strings e.g.
int subsLeftToPreform = num;
StringBuffer buf = new StringBuffer(str);
int ind = str.indexOf(variable);
while (ind >= 0 && subsLeftToPreform-- != 0) {
    buf.replace(ind, ind + variable.length(), value);
    ind = buf.toString().indexOf(variable);
return buf.toString();
...
Stringsubstitute(String string, String pattern, String replacement)
Replace all occurrences of pattern in the specified string with replacement.
int start = string.indexOf(pattern);
while (start != -1) {
    StringBuffer buffer = new StringBuffer(string);
    buffer.delete(start, start + pattern.length());
    buffer.insert(start, replacement);
    string = new String(buffer);
    start = string.indexOf(pattern, start + replacement.length());
return string;