Search code examples
javareplaceall

How to know if this Java's string.replaceAll is executed or not?


if i have some string e.g:

String s = "This is a string";

And now i perform this action:

s = s.replaceAll("This","What");

It works, with s = "What is a string"

fine, but now if the expression was not a match:

s = s.replaceAll("junk","What");

s remains what it was before, i.e s = "This is a string";


I want to know, without equating these strings, is there a way to know if s.replaceAll really performed some action or not??


Solution

  • would it be ok if you try

    if(s.contains("junk")) 
      s.replaceAll("junk", "What"); 
    else 
      //you know it hasn't executed!