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 modifierservi
- a literal char sequence$0
- a backreference to the whole match value. It makes sure you insert theservi
with the matched case (no need to hardcode it inside the replacement as in the original code).
Related Topics
Displaying Currency in Indian Numbering Format
What Is the Significance of Url-Pattern in Web.Xml and How to Configure Servlet
Method Chaining + Inheritance Don't Play Well Together
How to Convert a String to a Date Using Simpledateformat
How to Handle Pop-Up in Selenium Webdriver Using Java
What Causes a Java.Lang.Stackoverflowerror
How to Select/Get Drop Down Option in Selenium 2
How to Annotate MySQL Autoincrement Field with JPA Annotations
Jdbc Class.Forname VS Drivermanager.Registerdriver
How to Make My String Comparison Case-Insensitive
How to Launch a Completely Independent Process from a Java Program
Jcombobox Selection Change Listener
Why Won't This Generic Java Code Compile
How to Set Up Jax-Rs Application Using Annotations Only (No Web.Xml)
Spring Data JPA - How to Combine Multiple and and or Through Method Name