4

I want to replace multiple case insensitive strings from a String.

I could have used: org.apache.commons.lang.StringUtils.replaceEach(text, searchList, replacementList)

but is works for case sensitive strings.

Is there a similar method which works for case insensitive strings?

static String[] old = {"ABHISHEK","Name"};
static String[] nw = {"Abhi","nick name"};
static String s="My name is Abhishek";
System.out.println(StringUtils.replaceEach(s, old, nw));

Output:

My name is Abhishek

Expected:

My nick name is Abhi
Abhishek
  • 688
  • 6
  • 21
  • 2
    "Is there a similar method which works for case insensitive strings?" Nope. You'll need to write one yourself. – Michael Jan 22 '19 at 10:43

3 Answers3

6

You can try to use regex to archive it

Example

String str = "Dang DANG dAng dang";
//replace all dang(ignore case) with Abhiskek
String result = str.replaceAll("(?i)dang", "Abhiskek");
System.out.println("After replacement:" + "   " + result);

Result:

After replacement: Abhiskek Abhiskek Abhiskek Abhiskek

EDIT


String[] old = {"ABHISHEK","Name"};
String[] nw = {"Abhi","nick name"};
String s="My name is Abhishek";
//make sure old and nw have same size please
for(int i =0; i < old.length; i++) {
    s = s.replaceAll("(?i)"+old[i], nw[i]);
}
System.out.println(s);

Result:

My nick name is Abhi

Basic ideal: Regex ignore case and replaceAll()

From the comment @flown (Thank you) you need to use

str.replaceAll("(?i)" + Pattern.quote(old[i]), nw[i]);

Because regex treats some special character with a different meaning, ex: . as any single character

So using the Pattern.quote will do this.

Dang Nguyen
  • 1,209
  • 1
  • 17
  • 29
3

Since you're already using StringUtils, StringUtils.replaceIgnoreCase is a good candidate to go with. Worth to mention that the version 3.5+ required.

public static String replaceIgnoreCase(String text,
                                       String searchString,
                                       String replacement)

Case insensitively replaces all occurrences of a String within another String.

A null reference passed to this method is a no-op.

 StringUtils.replaceIgnoreCase(null, *, *)        = null
 StringUtils.replaceIgnoreCase("", *, *)          = ""
 StringUtils.replaceIgnoreCase("any", null, *)    = "any"
 StringUtils.replaceIgnoreCase("any", *, null)    = "any"
 StringUtils.replaceIgnoreCase("any", "", *)      = "any"
 StringUtils.replaceIgnoreCase("aba", "a", null)  = "aba"
 StringUtils.replaceIgnoreCase("abA", "A", "")    = "b"
 StringUtils.replaceIgnoreCase("aba", "A", "z")   = "zbz"

In your case:

String[] old = {"ABHISHEK", "Name"};
String[] nw = {"Abhi", "nick name"};
String s = "My name is Abhishek";

for (int i = 0; i < old.length; i++) {
    s = StringUtils.replaceIgnoreCase(s, old[i], nw[i]);
}

System.out.println(s);

Output:

My nick name is Abhi

You can even create a helper method if you going to use it frequently:

public static String replaceIgnoreCase(final String s, final String searchList[], final String replacementList[]) {
    if (searchList.length != replacementList.length)
        throw new IllegalArgumentException("Search list and replacement list sizes do not match");

    String replaced = s;
    for (int i = 0; i < searchList.length; i++) {
        replaced = StringUtils.replaceIgnoreCase(s, searchList[i], replacementList[i]);
    }

    return replaced;
}

And use it like you would use a library call:

s = replaceIgnoreCase(s, old, nw);
J-Alex
  • 6,881
  • 10
  • 46
  • 64
  • The method replaceIgnoreCase(String, String, String) is undefined for the type StringUtils. I'm using commons-lang-2.6.jar – Abhishek Jan 22 '19 at 11:22
0

in JAVA, Strings are case sensitive; if you want to use them insensitive you should use proper methods. for example equals() is really different with equalsIgnoreCase().

You can use:

 String.equalsIgnoreCase()

Or:

compareToIgnoreCase()

and if these methods return a proper data you can use them to replace them with String which you want.

Majid Roustaei
  • 1,556
  • 1
  • 20
  • 39