I have a problem with removing everything after the last slash of URL in JAVA For instance, I have URL:
http://stackoverflow.com/questions/ask
n' I wanna change it to:
http://stackoverflow.com/questions/
How can I do it.
replaceAll("/","");
Use the String. replace() method to remove a trailing slash from a string, e.g. str. replace(/\/+$/, '') . The replace method will remove the trailing slash from the string by replacing it with an empty string.
A character preceded by a backslash (\) is an escape sequence and has a special meaning to the compiler. The following table shows the Java escape sequences. Inserts a tab in the text at this point. Inserts a backspace in the text at this point.
You can try this
String str="http://stackoverflow.com/questions/ask"; int index=str.lastIndexOf('/'); System.out.println(str.substring(0,index));
IF you want to get the last value from the uRL
String str="http://stackoverflow.com/questions/ask"; System.out.println(str.substring(str.lastIndexOf("/")));
Result will be "/ask"
If you want value after last forward slash
String str="http://stackoverflow.com/questions/ask"; System.out.println(str.substring(str.lastIndexOf("/") + 1));
Result will be "ask"
Try using String#lastIndexOf()
Returns the index within this string of the last occurrence of the specified character.
String result = yourString.subString(0,yourString.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