How to String format urls that contain path parameter

format, java, string

Solution

Since the placeholders are for `String` values, you need to use `%s` instead of just `%`.

final String URL = "https://www.someurl.com/v2/path/%s?key=%s";

Problem

I have the url `https://www.someurl.com/v2/path/[PARAM1]?key=[PARAM1]2`. I want to do the following ``` final String URL = "https://www.someurl.com/v2/path/%?key=%"; String val = String.format(URL, "details", "testing"); System.out.println(val); ``` But I am getting an error ``` Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = '?' at java.util.Formatter.checkText(Formatter.java:2547) at java.util.Formatter.parse(Formatter.java:2533) at java.util.Formatter.format(Formatter.java:2469) at java.util.Formatter.format(Formatter.java:2423) at java.lang.String.format(String.java:2845) at split.StringStuff.main(StringStuff.java:22) ``` How might I make it work?

Original source

Related problems