4

I have a base url string and another string which should be appended to the base url to get the exact url to request. currently i am using string manipulation to achieve the result. The code i implemented is as follows

private String getUrl(String base, String className){
        try{
            if(!base.endsWith("/")){
                base= base + "/";
            }
            base= base+ base;
            return base;
        }

Is there any inbuilt method to concat the two string directly?

1
  • I asked about any url methods to do that like as answer specified by @isnot2bad Commented Dec 2, 2013 at 10:27

1 Answer 1

4

You can use the java.net.URI class:

URI baseURI = new URI(base + "/");
URI fullURI = baseURI.resolve(className);

URL url = fullURI.toURL(); // as URL
String urlString = fullURI.toString(); // as String

Especially if you can pre-create the base URI once and reuse it multiple times!

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

Comments

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.