Java Utililty Methods String Replace

List of utility methods to do String Replace

Description

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

Method

StringreplaceAll(String source, String toReplace, String replacement)
replace All
int idx = source.lastIndexOf(toReplace);
if (idx != -1) {
    StringBuffer ret = new StringBuffer(source);
    ret.replace(idx, idx + toReplace.length(), replacement);
    while ((idx = source.lastIndexOf(toReplace, idx - 1)) != -1) {
        ret.replace(idx, idx + toReplace.length(), replacement);
    source = ret.toString();
...
StringreplaceEmitKeys(String map, Collection newKeys)
replace Emit Keys
Collection<String> oldKeys = getEmitKeys(map);
return map.replace(buildString(oldKeys), buildString(newKeys));
StringreplaceFirst(String input, String search, String replacement)
Replaces the first search-string in the given input.
if (input == null || search == null || replacement == null) {
    throw new IllegalArgumentException("TextUtil.replace: given input parameters must not be null.");
int pos = input.indexOf(search);
if (pos == -1) {
    return input;
StringBuffer buffer = new StringBuffer();
...
StringreplaceIgnoreCase(String line, String oldString, String newString)
replace Ignore Case
if (line == null)
    return null;
String lcLine = line.toLowerCase();
String lcOldString = oldString.toLowerCase();
int i = 0;
if ((i = lcLine.indexOf(lcOldString, i)) >= 0) {
    char line2[] = line.toCharArray();
    char newString2[] = newString.toCharArray();
...
StringreplaceIgnoreCase(String source, String strBeReplace, String strReplaced)
replace Ignore Case
if (isEmpty(source) || isEmpty(strBeReplace) || strReplaced == null)
    return source;
StringBuffer buf = new StringBuffer(source.length());
int start = 0, end = 0;
String strReplacedCopy = strBeReplace.toUpperCase();
String sourceCopy = source.toUpperCase();
while ((end = sourceCopy.indexOf(strReplacedCopy, start)) != -1) {
    buf.append(source.substring(start, end)).append(strReplaced);
...
StringreplaceNonPrintableAsciiCharacters(String str)
Replaces the non-printable ASCII characters with readable counterparts in square brackets, e.g.
byte[] bytes = str.getBytes();
return replaceNonPrintableAsciiCharacters(bytes, 0, bytes.length);
StringreplaceOnce(String template, String placeholder, String replacement)
replace Once
if (template == null) {
    return template; 
int loc = template.indexOf(placeholder);
if (loc < 0) {
    return template;
} else {
    return new StringBuffer(template.substring(0, loc)).append(replacement)
...
StringreplaceSign(String sourceStr, char startc, char endc, String replaceStr)
replace Sign
char[] cs = sourceStr.toCharArray();
int i = 0;
int index = 0;
int startindex = -1;
int endindex = -1;
List<int[]> list = new ArrayList<int[]>();
for (char c : cs) {
    if (c == startc) {
...
StringreplaceSpace(String str, int actualLength)
replace Space
char[] characters = new char[str.length()];
characters = str.toCharArray();
ArrayList<Integer> positions = new ArrayList<Integer>();
for (int i = 0; i < actualLength; i++) {
    if (str.charAt(i) == ' ') {
        if (positions.isEmpty())
            positions.add(i);
        else {
...
StringreplaceUnsafeNamespaceDelimiters(String input)
* Replaces non-alphanumeric namespace delimiters in input with an alphanumeric form that can be handled by Vampire and other provers.
String output = input;
try {
    if (isNonEmptyString(output)) {
        String safe = ("$1" + getSafeNamespaceDelimiter() + "$2");
        List<String> unsafe = Arrays.asList(getKifNamespaceDelimiter(), getW3cNamespaceDelimiter());
        for (String delim : unsafe) {
            output = output.replaceAll("(\\w)" + delim + "(\\w)", safe);
} catch (Exception ex) {
    ex.printStackTrace();
return output;