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

Posted on 2009-04-16
Last Modified: 2013-12-14
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?
Question by:andy148
    LVL 6

    Accepted Solution

    str = str.replaceAll("(?i)test", "#");

    Author Comment

    Seems to be working great. Thanks!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project ( automates most of the tasks discussed in this article. You can even fin…
    Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
    The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
    The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now