Create a URL from variables

Viewed 40

I have this URL:

String template = "https://{subdomain}.apache.com/t/information/{information_service}";

I would like to know how can I replace the values {subdomain} and {information_service} to build a new URL. Something like this

String subdomain = "newHouse";
String information_service = "rooms";

Note: I will get different values for each iteration and session, this is just an example

Output

 String URL = "https://newHouse.apache.com/t/information/rooms";

I tried with this example

        URIBuilder builder = new URIBuilder()
            .setScheme("http")
            .setHost("apache.org")
            .setPath("/shindig")
            .addParameter("helloWorld", "foo&bar")
            .setFragment("foo");
    builder.toString();

But I wonder if there are something more directly to replace the value of placeholders

1 Answers

for your task you can override toString method inside uiBuilder and inside that you can use template.replace(subdomain, {subdomain})

example:

@Override
    public String toString() {
       template  = 'https//{subdomain}'
       String url = template.replace(getSubdomain, {subdomain});
       return url
    }

According to the number of variables you can extend the above code. I do not sure whether my answer match to your context.

Related