1

I get a string-template and a variable-length argument list. I need I need to insert arguments into the template and send the result.

For instance:

template: "%1s test %2s test %1s"

args: "CAT", "DOG"

Result: "CAT test DOG test CAT"

I tried to do it like this. But I got an error, because in fact, I'm trying to execute the string String.format("%1s test %2s test %1s", "value") which is really wrong.


    public static void main(String[] args) {
        getStringFromTemplate("%1s test %2s test %1s", "CAT", "DOG");
    }

    public void getStringFromTemplate(String template, String... args){
        ArrayList<String> states = new ArrayList<>();
        Collections.addAll(states, args);
        String s;
        Iterator<String> iter = states.iterator();
        while(iter.hasNext()){
            s = String.format("%1s test %2s test %1s", iter.next());
        }
        rerurn s;
    }

2 Answers 2

3

String.format takes as the second argument varargs, so you could just rewrite your code like this:

public static String getStringFromTemplate(String template, String ...args) {
    return String.format(template, args);
}

Also, if you want to use one parameter many times, you should change your template String:

template = "%1$s test %2$s test %1$s";

You can find understandable tutorial here.

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

8 Comments

The template isn't a proper format string, it seems to me OP is supposed to manually replace the placeholders by the appropriate value
@Aaron surely this isn't correct, I should be more attentive
@ginkul - This is an incorrect (or partially correct) solution. Please check my answer for the correct solution.
@ArvindKumarAvinash yes, this is really not my day... I've fixed it and now it definitely works. Despite my obvious mistakes, which I made, I'd recommend you to use built-it functionality of Java, as it is more generalized and takes less time to implement
@ginkul - In fact, it's your day :) despite partially correct answer, it was accepted and upvoted while my answer didn't even get anyone's attention. There have been many days in my life when even my weak answers got accepted and upvoted. On the other hand, there have been days when many of my elegant answers were ignored. This is the pleasure of life - sometimes happiness and sadness! Wish you success!
|
0

I believe you're looking for this :

public String getStringFromTemplate(String template, String... args){
    for (int i=0; i<args.length; i++) {
        String placeholder = "%" + (i+1) + "s";
        String actualValue=args[i];
        template = template.replaceAll(placeholder, actualValue);
    }
    return template;
}

This code iterates over your arguments, derives a placeholder from their index in the args array and replace all occurences of this placeholder by the actual value of the argument in the template string.

You can try it here

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.