I want to remove some String from my original path, but I cant replace the another string from my original String
This is my code
String path="contentPath =C/Users/consultant.swapnilb/Desktop/swapnil=Z:/Build6.0/Digischool/";
String a=path.substring(path.lastIndexOf("="), path.length());
path.replace(a, "");
System.out.println("a---"+a);
System.out.println("path---"+path);
I just want to remove =Z:/Build6.0/Digischool/ from my original path.
See String#replace:
public String replace(CharSequence target, CharSequence replacement)
↑
It returns a new object of type String, you should assign the result:
path = path.replace(a, "");
However, you can simply do:
path = path.substring(0, path.lastIndexOf("="));
First of all, since Strings are immutable in Java, you have to re-assign the changes in a String to another reference:
path = path.replace(a, "");
Secondly, you are doing extra job out there. You can replace these lines:
String a=path.substring(path.lastIndexOf("="), path.length());
path.replace(a, "");
with:
path = path.substring(0, path.lastIndexOf("="));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With