3

I have the following String text:

/Return/ReturnData/IRS1040/DependentWorksheetPP[1]/DependentCodePP

I'd like to strip off the [1] index so I just have:

/Return/ReturnData/IRS1040/DependentWorksheetPP/DependentCodePP

How can I accomplish this in Java?

string.replaceAll("[?]","");

This doesn't seem to work.

Any help or info would be much appreciated

2
  • 1
    Strings are immutable in Java, 'replaceAll' will return a new string that has the replacements. See String.replaceAll. Possible duplicate: stackoverflow.com/questions/10951333/…. Is this the issue you're having? Commented Nov 5, 2015 at 4:59
  • Replace \[\d+\] by empty string Commented Nov 5, 2015 at 5:00

2 Answers 2

5

First, in Java, String is immutable (so be sure to assign the result of replaceAll). Next, the [ and ] are meaningful in a regular expression (escape them). And \\d+ is one or more digit. Something like,

String str = "/Return/ReturnData/IRS1040/DependentWorksheetPP[1]/" + "DependentCodePP"; str = str.replaceAll("\\[\\d+\\]", ""); System.out.println(str); 

Output is

/Return/ReturnData/IRS1040/DependentWorksheetPP/DependentCodePP 
Sign up to request clarification or add additional context in comments.

Comments

3
string.replaceAll("\\[.*?\\]",""); 

You need to escape [] as they are special characters in regex.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.