Android 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

Stringreplace(String s, String[] oldSubs, char[] newSubs)
replace
if ((s == null) || (oldSubs == null) || (newSubs == null)) {
    return null;
if (oldSubs.length != newSubs.length) {
    return s;
for (int i = 0; i < oldSubs.length; i++) {
    s = replace(s, oldSubs[i], String.valueOf(newSubs[i]));
...
Stringreplace(String s, char oldSub, String newSub)
replace
if ((s == null) || (newSub == null)) {
    return null;
char[] c = s.toCharArray();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < c.length; i++) {
    if (c[i] == oldSub) {
        sb.append(newSub);
...
Stringreplace(String s, char oldSub, char newSub)
replace
return replace(s, oldSub, Character.valueOf(newSub));
Stringreplace(String source, CharSequence target, CharSequence replacement)
replace
if (source != null) {
    return source.replace(target, replacement);
return null;
StringreplaceChar(String source, String subject, String object)
replace Char
StringBuffer rtnStr = new StringBuffer();
String preStr = "";
String nextStr = source;
String srcStr = source;
char chA;
for (int i = 0; i < subject.length(); i++) {
    chA = subject.charAt(i);
    if (srcStr.indexOf(chA) >= 0) {
...
StringreplaceSpaceCharacter(String string)
replace Space Character
if (null == string || "".equals(string)) {
    return "";
return string.replace(" ", "");
Stringcollapse(String str, String chars, String replacement)
Replaces any string of matched characters with the supplied string.

This is a more general version of collapseWhitespace.

if (str == null) {
    return null;
StringBuilder newStr = new StringBuilder();
boolean prevCharMatched = false;
char c;
for (int i = 0; i < str.length(); i++) {
    c = str.charAt(i);
...
StringcollapseControlChars(String str, String replacement)
Returns a string with all sequences of ISO control chars (0x00 to 0x1F and 0x7F to 0x9F) replaced by the supplied string.
if (str == null) {
    return null;
StringBuilder newStr = new StringBuilder();
boolean prevCharMatched = false;
char c;
for (int i = 0; i < str.length(); i++) {
    c = str.charAt(i);
...
StringstringReplace(String str, String what, String replacement)
string Replace
int i = str.indexOf(what);
if (i >= 0) {
    int j = 0;
    int whatLen = what.length();
    StringBuilder result = new StringBuilder();
    do {
        result.append(str.substring(j, i));
        result.append(replacement);
...