2
String Checkout = D:\ifs\APP\Checkout
String DeleteLine = D:\IFS\APP\Checkout\trvexp\client\Ifs.App\text.txt

Note the ifs and IFS in both Strings. I want to replace the Checkout String in the Deleted Line

So the final String would look like this:

\trvexp\client\Ifs.App\text.txt

Following is what I have tried, but obviously due to Case Sensitivity, the string won't get replaced. Any Solution or a work around for this?

String final = DeleteLine.replace(Checkout, "");
asked Jun 26, 2015 at 6:16
3
  • 1
    Do you know which characters may be uppercase? Check stackoverflow.com/q/5054995/928952 and stackoverflow.com/q/15613209/928952 Commented Jun 26, 2015 at 6:19
  • 1
    Add a "(?i)" to your checkout string and then try replaceAll() Commented Jun 26, 2015 at 6:20
  • @Danielson Only the word IFS Commented Jun 26, 2015 at 6:23

4 Answers 4

9

String.replace() doesn't support regex. You need String.replaceAll().

DeleteLine.replaceAll("(?i)" + Pattern.quote(Checkout), "");
answered Jun 26, 2015 at 6:23
Sign up to request clarification or add additional context in comments.

2 Comments

you may use replaceFirst also.
Thanks a lot. This worked :) I'll accept the answer in a minute
3

Put (?i) in the replaceAll method's regular expression:

String finalString = DeleteLine.replaceAll("(?i)" + Checkout, "");
answered Jun 26, 2015 at 6:20

1 Comment

It's probably worth using Pattern.quote with Checkout as well, otherwise it won't mean what the OP really wants it to.
2

You can do this:

String Checkout = "D:\\\\ifs\\\\APP\\\\Checkout";
String DeleteLine = "D:\\IFS\\APP\\Checkout\\trvexp\\client\\Ifs.App\\text.txt";
String f = DeleteLine.replaceFirst("(?i)"+Checkout, "");
answered Jun 26, 2015 at 6:26

Comments

2

Alternatively, if youi want the pattern on a specific portion you can do it manually. You can declare the checkout Sting as:

String Checkout= \Q(?i)D:\ifs\APP\Checkout\E

as

\Q means "start of literal text"
\E means"end of literal text"

and then do the replace

String final = DeleteLine.replace(Checkout, "");
answered Jun 26, 2015 at 6:35

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.