4

I have a string template like this:

http://server/{x}/{y}/{z}/{t}/{a}.json

And I have the values:

int x=1,y=2,z=3,t=4,a=5;

I want to know which is the efficent way to replace the {x} to the value of x, and so does y,z,t,z?

7
  • Do you know the exact order of the parameters in advanced, or the template could also be something like http://server/{z}/{x}/{y}.json? Commented May 13, 2013 at 7:28
  • 1
    The order is not sure, since the url may be something like this http://server/{y}.json?x={x}&y={y} Commented May 13, 2013 at 7:29
  • I read all the fllowing answsers, and it seems that all of them will require the order of the placeholders? Commented May 13, 2013 at 7:34
  • That's right. I think there's no built in way in Java to do it, and you should manually iterate over the string and replace the '{?}' with the accurate value. Commented May 13, 2013 at 7:36
  • @hguser No. You can use http://server/{1}.json?x={0}&y={1}. Commented May 13, 2013 at 7:36

6 Answers 6

16
String template = "http://server/%s/%s/%s/%s/%s.json";
String output = String.format(template, x, y, z, t, a);
Sign up to request clarification or add additional context in comments.

Comments

4

Another way to do it (C# way ;)):

MessageFormat mFormat = new MessageFormat("http://server/{0}/{1}/{2}/{3}/{4}.json");
Object[] params = {x, y, z, t, a};
System.out.println(mFormat.format(params));

OUTPUT:

http://server/1/2/3/4/5.json

2 Comments

While int x=27315, then the formatted string is something like .../27,315/....... What is the problem?
You could use Object[] params = {Integer.toString(x)}; or Object[] params = {"" + x};.
4

Use MessageFormat.java

MessageFormat messageFormat = new MessageFormat("http://server/{0}/{1}/{2}/{3}/{4}.json");
Object[] args = {x,y,z,t,a};
String result = messageFormat.format(args);

Comments

3
http://server/{x}/{y}/{z}/{t}/{a}.json

If you can change that to http://server/{0}/{1}/{2}/{3}/{4}.json you can use MessageFormat:

String s = MessageFormat.format("http://server/{0}/{1}/{2}/{3}/{4}.json", x, y, z, t, a);

Comments

1

To replace the placeholders exact how you use them in example, you can use StrinUtils.replaceEach

org.apache.commons.lang.StringUtils.replaceEach(
"http://server/{x}/{y}/{z}/{t}/{a}.json",
new String[]{"{x}","{y}","{z}","{t}","{a}"},
new String[]{"1","2","3","4","5"});

However, MessageFormat will be more efficient, but requiring to replace x with 0, y with 1 etc.

If you change your format to

"http://server/${x}/${y}/${z}/${t}/${a}.json",

you can consider using Velocity, which has parser specialized on finding ${ and } occurences.

The most efficient way would be to write own parser searching for next { occurence, than }, than replacing the placeholder.

Comments

0

This Could be probable answer.

    String url= "http://server/{x}/{y}/{z}/{t}/{a}.json";
    int x=1,y=2,z=3,t=4,a=5;

    url = url.replaceAll("\\{x\\}", String.valueOf(x));
    url = url.replaceAll("\\{y\\}", String.valueOf(y));
    url = url.replaceAll("\\{z\\}", String.valueOf(z));
    url = url.replaceAll("\\{t\\}", String.valueOf(t));
    url = url.replaceAll("\\{a\\}", String.valueOf(a));

    System.out.println("url after : "+ url);

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.