10

For example I want to implement class with method

public class Logger {

    public void info(String message, String[] params) {
    }
}

If input is

new Logger().info("Info: param1 is ? , param2 is ?", new  String[] {"a", "b"});

Output must be

Info: param1 is a , param2 is b

What is the easiest way to implement it?

1
  • 2
    Is there a reason you can't use the standard String.Format method?
    – Paolo
    Sep 2, 2011 at 7:27

4 Answers 4

23

You can use the String.format(String format, Object ... args) method for this. Instead of using a ?, you can do C style %x format, where x can be d (for int), s (for string), etc.

Example.

Also, you can view the Formatter.format class method. It shows you all formatting flags acceptale for formatting (String.format() method uses Formatter to do the formatting).

1
  • 1
    Your link no longer works. This is why Stack Overflow asks you to include relevant material within your Answer. Aug 3, 2018 at 23:37
5

The following code does as you require.

public class Logger {

    public static void main(String[] args) {
        new Logger().info("Info: param1 is ?, param2 is ?", new String[] { "a", "b" });
        new Logger().info("Info: param1 is ?, param2 is ?", "a", "b");
    }

    public void info(final String message, final String... args) {
        System.out.printf(message.replace("?", "%s") + "%n", args);
    }

}

However, you should consider using correct formatting place-holders instead of '?'. For instance:

 public class Logger {

    public static void main(String[] args) {
        new Logger().info("Info: param1 is %s, param2 is %s", new String[] { "a", "b" });
        new Logger().info("Info: param1 is %s, param2 is %s", "a", "b");
    }

    public void info(final String message, final String... args) {
        System.out.printf(message + "%n", args);
    }

}

Both versions will print:

Info: param1 is a, param2 is b
Info: param1 is a, param2 is b
2

If you don't have to worry about escaping the '?', and you can't use printf() style format strings, you could get away with:

public void info(String message, String[] params) {
    System.out.println(
        String.format(message.replace("?", "%s"), params)
    );
}

Like so:

info("apple is to ? as eve is to ?",
    new String[] { "banana", "noon" });

Which prints: apple is to banana as eve is to noon


If you need more sophisticated logic, such as escaping, then you'll probably want a full-blown regexp Pattern-based search and replace.

2
  • Ok, the output prints only to System.out. What if the OP wants to log the error to a file using a logging framework? Sep 2, 2011 at 7:40
  • @The Elite Gentleman, then I think the OP should be using a logging framework instead of trying to write one himself/herself. Sep 2, 2011 at 14:41
0

in a loop on params[] use regexp to find every '?' in your string and substitute it with the current param. Beware of nulls and find a proper way to escape ? (guess \? will give some trouble, better ??)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.