How to Replace Case-Insensitive Literal Substrings in Java

How to replace case-insensitive literal substrings in Java

String target = "FOOBar";
target = target.replaceAll("(?i)foo", "");
System.out.println(target);

Output:

Bar

It's worth mentioning that replaceAll treats the first argument as a regex pattern, which can cause unexpected results. To solve this, also use Pattern.quote as suggested in the comments.

Replace String ignoring case in Java

String.replace() doesn't support regex. You need String.replaceAll().

DeleteLine.replaceAll("(?i)" + Pattern.quote(Checkout), "");

How to do a case-insensitive string replacement

Could use a regular expression.
Just add (?i) before your string to ignore case.

So for example:

multiword.getString().replaceAll ( "(?i)radha", "sai");

Java: Replace substring with bsubstring/b case insensitive

You can use back-referencing to replace the string you want. Take a look at this question for more explanations about back-referencing.

Taking that into account, this should do the trick:

s.replaceAll("(?i)(" + searchTerm + ")", "<b>$1</b>")

Multiple case insensitive strings replacement

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.

Case Insensitive variable for String replaceAll(,) method Java

Just add the "case insensitive" switch to the regex:

html.replaceAll("(?i)"+label, "WINDOWS");

Note: If the label could contain characters with special regex significance, eg if label was ".*", but you want the label treated as plain text (ie not a regex), add regex quotes around the label, either

html.replaceAll("(?i)\\Q" + label + "\\E", "WINDOWS");

or

html.replaceAll("(?i)" + Pattern.quote(label), "WINDOWS");

Java replace content which use whitespace as delimiter in a string(case insensitive)

Using replaceAll its a good aproach. I think that you must look for regex, in this link for example.

This is an implementation that apply for your examples, but maybe you need to make some changes if you want something a bit different.

text = text.replaceAll("(?i) " + keyWord + " ", " zoo ");

In this case,

keyWord = "foo";

How to check a String for multiple condition in Java?

Try it like this. The (?i) is a flag that says ignore case.

String s = "Other text Data daTa DATA data dATa";
s = s.replaceAll("(?i)data", "Data");
System.out.println(s);

prints

Other text Data Data Data Data Data

Java String is getting replaced with case sensitivity

Use

String stringValue = "Servicing";
if (stringValue != null && !stringValue.isEmpty()) {
String newData = stringValue.replaceAll("(?i)servi", "<mark>$0</mark>");
System.out.println(newData);
}

See the Java demo

Details:

  • (?i) - case insensitive inline modifier
  • servi - a literal char sequence
  • $0 - a backreference to the whole match value. It makes sure you insert the servi with the matched case (no need to hardcode it inside the replacement as in the original code).


Related Topics



Leave a reply



Submit