Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: What is the easiest way to get entire URL except last part?

Tags:

java

consider the url is

http://www.google.com/a/b/myendpoint

All I want is the following

http://www.google.com/a/b/

One approach is to split the String url and join all the components except last one.

I am thinking if there could be a better way?

like image 404
daydreamer Avatar asked Dec 26 '22 20:12

daydreamer


1 Answers

You can use lastIndexOf():

url.substring(0, url.lastIndexOf('/') + 1)

String url = "http://www.google.com/a/b/myendpoint";
System.out.println(url.substring(0, url.lastIndexOf('/') + 1));
http://www.google.com/a/b/
like image 184
arshajii Avatar answered Dec 28 '22 08:12

arshajii