In C# there is a method to write a string to the Console.
it is Console.WriteLine("Hello {0} My name is {1}", "World", "John");
this would return
Hello World My name is John
How can i recreate such a method structure in java. So that i can pass in unlimited amount of the parameters in the end of my method and get it placed in the right indexes?
Any help would be greatly appreciated
// EDIT
Maybe i have not explained well enough. I do not need the method to make a console output. I just want to know how can i recreate a structure in which i can pass as many parameters as i want and get it placed in the right place. For example
movie.setPlot(“This movie is {0} and gets a rating of {1}”, “FUN”, “6 Thumbs up”);
which would set the the plot varialbe for a movie to
This movie is FUN and gets a rating of 6 Thumbs up
// EDIT 2
End Result:
private static final String PREFIX = "AwesomeApp";
public static void e(String TAG, String msg){
android.util.Log.e(PREFIX + " >> " +TAG, msg);
}
public static void e(String TAG, String msg, Object...args){
e(TAG, String.format(msg, args));
}
You can use
var-argsto handle an indeterminate number of parameters: