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

StringreplaceSpace(String str)
replace Space
if (str == null) {
    return str;
return str.replace(" ", "");
StringreplaceStr(String number)
replace Str
if ("".equals(number) || number == null) {
    return "0";
} else {
    return number;
StringreplaceString(String s, Map replacement)
replace String
if (s != null && !s.equals("") && replacement != null) {
    for (String key : replacement.keySet()) {
        String actualKey = "\\{" + key.toUpperCase(Locale.US)
                + "\\}";
        s = s.replaceAll(actualKey, replacement.get(key));
return s;
...
StringreplaceWords(String src, String targetValue, String newValue)
replace Words
if (targetValue.equals("")) {
    throw new IllegalArgumentException(
            "Old pattern must have content.");
StringBuffer result = new StringBuffer();
int startIdx = 0;
int idxOld = src.indexOf(targetValue, startIdx);
if (idxOld >= 0) {
...
StringreplaceWordsAll(String src, String targetValue, String newValue)
replace Words All
if (targetValue.equals("")) {
    throw new IllegalArgumentException(
            "Old pattern must have content.");
StringBuffer result = new StringBuffer();
int startIdx = 0;
int idxOld = 0;
while ((idxOld = src.indexOf(targetValue, startIdx)) >= 0) {
...
StringreplaceBlank(String str)
replace Blank
String dest = "";
if (str != null) {
    str = str.replace(" ", "+");
    Pattern p = Pattern.compile("\\s*|\t|\r|\n");
    Matcher m = p.matcher(str);
    dest = m.replaceAll("");
return dest;
...
StringreplaceBlank(String str)
replace Blank
String dest = "";
if (str != null) {
    Pattern p = Pattern.compile("\r");
    Matcher m = p.matcher(str);
    dest = m.replaceAll("");
return dest;
StringreplaceBlank(String str)
replace Blank
String dest = "";
if (str != null) {
    Pattern p = Pattern.compile("\t|\r|\n");
    Matcher m = p.matcher(str);
    dest = m.replaceAll("");
return dest;
StringreplaceBlank(String str)
replace Blank
Pattern pattern = Pattern.compile("\\s*|\t|\r|\n");
Matcher matcher = pattern.matcher(str);
return matcher.replaceAll("");
StringremoveIllegal(String input)
remove Illegal
String invalidXMLChars = "[^[\\u0009\\u000A\\u000D][\\u0020-\\uD7FF][\\uE000-\\uFFFD][\\u10000-\\u10FFFF]]";
return input.replaceAll(invalidXMLChars, "?");