0

I have a String holding a URL in this format: http://hello.world.com/service/sps/f4c0e810456t And I would like to extract the last part of the URL, i.e. f4c0e810456t.

I can do it with substrings:

    System.out.println(s.substring(s.lastIndexOf("/") + 1, s.length()));

Or regexp however looking for something more elegant using URL/URI objects but couldn't find something.

Any ideas...?

1

2 Answers 2

1

If you can change the URL to "http://hello.world.com/service/sps/?f4c0e810456t" then you could use the getQuery() method (both on URL and URI).

Sign up to request clarification or add additional context in comments.

Comments

0

Example with URL and split (it wrap regular expression for you):

    String address = "http://hello.world.com/service/sps/f4c0e810456t";
    URL url = new URL(address);

     String [] str = url.getPath().split("/");
     String result = str[str.length-1];

1 Comment

I expected something more "built in" to these Objects that I might be missing but that will do. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.