The following program shows how to replace a string by ignoring its case:
public class CaseInsensitiveStringReplace {
public static void main(String[] args) {
String testString = "Replace me iRResPecTive of my case!!!";
System.out.println(testString.replaceAll("(?i)IRRESPECTIVE", ""));
}
}
Output:
Replace me of my case!!!
Explanation: String.replaceAll(,) has an overloaded version, which accepts the regular expression as first argument. (?i) says ignore case in regular expression terminology.
Note: Always make sure that regular expression related characters are escaped. For example, $ means at the end in regular expression terminology, which can be escaped by adding two backward slashes(\\).
public class CaseInsensitiveStringReplace {
public static void main(String[] args) {
String testString = "Replace me iRResPecTive of my case!!!";
System.out.println(testString.replaceAll("(?i)IRRESPECTIVE", ""));
}
}
Output:
Replace me of my case!!!
Explanation: String.replaceAll(,) has an overloaded version, which accepts the regular expression as first argument. (?i) says ignore case in regular expression terminology.
Note: Always make sure that regular expression related characters are escaped. For example, $ means at the end in regular expression terminology, which can be escaped by adding two backward slashes(\\).