• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3042
  • Last Modified:

How can I have Java's replaceAll function ignore case?

I'm working on a small project in Java and I can't seem to get the replaceAll function for strings to ignore the case. For example, I have
String str = "This is a Test string";
str = str.replaceAll("test", "#");

I've also tried
str = str.toLowerCase().replaceAll("test", "#");

The problem with that is it destroys the case. Is there anyway I can use replaceAll, and have it remove the specified string regardless of its case, without destroying the case of the whole sentence?
1 Solution
str = str.replaceAll("(?i)test", "#");
andy148Author Commented:
Seems to be working great. Thanks!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now