

Using String.format with multiple variablesįinally, here is an example of how to use multiple variables with the String format method: If it helps to see the format method used without the additional log.debug method, here’s an example where I use the String.format method to assign a similarly formatted String to another String: Java ‘sprintf’: assigning a formatted String to another String The value of that decimal comes from the variable that follows my string, in this case the RENAME_SUCCEEDED variable. The %d symbol is a placeholder that indicates that a decimal value (something like an int or long in Java) should be printed in place of this symbol. But, if you’re not familiar with this syntax, what happens in the line of code above is that the %d in my Java String is replaced by the value of the variable RENAME_SUCCEEDED, which in this case happens to be a constant in my class. If you’re familiar with the sprintf function from other languages like C, Perl, or Ruby, this syntax will look familiar. Here’s a quick example of how to use the String.format method to format a string before I pass it to a Log4J log.debug method: Log.debug( String.format("%s is %d years old, er, young", "Al", 45) ) įor more information on those examples, see the sections below. how to format a string with multiple variables

String status = String.format("The rename status is (%d)", RENAME_SUCCEEDED) format some text and assign it to a string Log.debug( String.format("The rename status is (%d)", RENAME_SUCCEEDED) ) You can format Java string output with the format method of the String class, which works like a “Java sprintf” method. Java String formatting FAQ: How can I format Java String output?
