-1

I'm trying to replace few characters matching an expression in a string by name inputPath such that if inputPath contains an expression like "json*" then "json*" has to removed from that string. I did like this:

String newPath = inputPath.replace("json*","");

When I print newPath, I am getting string with "json" being removed but "*" is not getting removed or replaced. But I need to get entire "json*" removed or replaced. I just dont understand why it's not replacing "*" but replacing "json" only.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
JavaUser
  • 33
  • 9
  • 1
    Your code should work. Can you provide an [MCVE](/help/mcve) showing the problem? – T.J. Crowder Dec 09 '15 at 12:07
  • @Tunaki Thank you solved the problem – JavaUser Dec 09 '15 at 12:13
  • 1
    @JavaUser Well, if it did, there's something very wrong because my comment was incorrect :D (and I have removed it). – Tunaki Dec 09 '15 at 12:13
  • If escaping solved the problem, as @Tunaki said, you weren't using `replace`. If you were using `replaceAll`, a trivial search of Stack Overflow would have found your solution ([here](http://stackoverflow.com/questions/14826143/string-replaceall-is-not-working), for instance). – T.J. Crowder Dec 09 '15 at 12:24
  • @T.J.Crowder I have solved the problem not using Tunaki's solution.I did like this to solve my problem: String replacedPattern = input.getPattern().replace("*","") String replacedPath = input.getPath().replace("*","");String finalPath=replacedPath.replace(replacedPattern, ""); – JavaUser Dec 10 '15 at 09:25
  • @T.J.Crowder The solution which you have provided in the link didn't work for me.So please remove the downvote as the solution which I have posted is not there in stackoverflow – JavaUser Dec 10 '15 at 09:31
  • @JavaUser: Never assume you know who voted. That said, the question as it stands right now asserts something doesn't work that **does** work and can trivially be verified to work ([example](http://ideone.com/RZOUOk)), so I'm not surprised it's been downvoted. I *am* surprised it hasn't been closed yet. – T.J. Crowder Dec 10 '15 at 09:36

2 Answers2

1

This is not true and unexpected in case for replace() in String class. Probably you are using replaceAll/replaceFirst, that takes the ReGex as input. I have reproduced what you are getting.

    String inputPath = "stackjson* overflow";
    String newPath = inputPath.replaceAll("json*", "");
    System.out.println(newPath);

Under that case, as * is a ReGex character so you need to escape them as using \\:

String newPath = inputPath.replaceAll("json\\*", "");

Tech Enthusiast
  • 279
  • 1
  • 5
  • 18
  • This isn't an answer to the question that was asked. It's an answer to a different question that's already asked-and-answered (repeatedly) on the site. Comment with a link, rather than posting an answer which is (as far as we know) irrelevant. – T.J. Crowder Dec 09 '15 at 12:22
0

I have figured out solution for the previous problem which I have posted: If I use :

String finalPath = input.getPath().replace("json*","")
           or
String finalPath = input.getPath().replace(input.getPattern(),"")

then I am getting output as /input/* with json removed but * not removed.Instead I did like this:

 String replacedPattern = input.getPattern().replace("*","");

 String replacedPath = input.getPath().replace("*","");

 String finalPath=replacedPath.replace(replacedPattern, "");

Now I got output as /input/ with json* removed which is expected to be.

JavaUser
  • 33
  • 9