We have method that looks like this :
public String getCommandString(Object...args) {
return this.code + " " + String.format(this.pattern, args);
}
Where this.code:int and this.pattern:String.
This method is usually called like this :
// cmd.code = 20
// cmd.pattern = "%1$s = %2$d"
String str = cmd.getCommandString("foo", 3); // -> "20 foo = 3"
and other string patterns (this is for a very simple text-based server-client program)
Now, is it possible for pattern to take into account a variable number of arguments, such as
// cmd.code = 20
// cmd.pattern = ???
String s1 = cmd.getCommandString("a", "b", "c"); // -> 20 a b c
String s2 = cmd.getCommandString("Hello", "world"); // -> 20 Hello world
String s3 = cmd.getCommandString("1", "2, "3", "4", "5", "6"); // -> 20 1 2 3 4 5 6
Assuming perhaps that each argument is of the same type (strings)? Or do I have to override the method and format the string manually? More specifically, I'm looking for a generic string pattern to format a variable number of arguments (of the same type). I remember making such a thing in C, but is this possible in Java?
StringUtils.join(args, ' ')is the way to go, but the I think the OP was just using that as an example. :)StringUtils) but usually turn to thejava.*packages first, if I can.