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, "");
-
1Do you know which characters may be uppercase? Check stackoverflow.com/q/5054995/928952 and stackoverflow.com/q/15613209/928952Danielson– Danielson2015年06月26日 06:19:59 +00:00Commented Jun 26, 2015 at 6:19
-
1Add a "(?i)" to your checkout string and then try replaceAll()Identity1– Identity12015年06月26日 06:20:01 +00:00Commented Jun 26, 2015 at 6:20
-
@Danielson Only the word IFSwishman– wishman2015年06月26日 06:23:27 +00:00Commented Jun 26, 2015 at 6:23
4 Answers 4
String.replace() doesn't support regex. You need String.replaceAll().
DeleteLine.replaceAll("(?i)" + Pattern.quote(Checkout), "");
2 Comments
replaceFirst also.Put (?i) in the replaceAll method's regular expression:
String finalString = DeleteLine.replaceAll("(?i)" + Checkout, "");
1 Comment
Pattern.quote with Checkout as well, otherwise it won't mean what the OP really wants it to.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, "");
Comments
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, "");