Home >Java >javaTutorial >sprintf Java
String.format() in Java is equivalent of the sprintf().The String. format() method returns a String object with the formatted string. The java string format() method is a build-in method, returns a formatted string based on the locale, format, and arguments passed to it. If the locale is not specified in the String. format() method, the default locale is used by calling Locale.getDefault().In the Java language, the format() method is similar to the sprintf() method in the c language. The String. format method can be used to assign or store a formatted String to another String.
ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
The string format() method comes in two flavors based on the parameter they accept –
1.
public static String format(String format, Object... args) { // code }
and
2.
public static String format(Locale locale, String format, Object... args) { // code }
Parameters:
The implementation of the String.format() method in Java
public static String format(String format, Object... args) { return new Formatter().format( format, args ).toString( ); }
The working of the String. format() method in Java The String. format() method in Java accepts three parameters. Suppose we have to print the number with the Filling with zeroes within the 10 specified widths. So we can use the String. format() method as “String.format(“The number is : %010d”, 13002);”, where the first parameter is the format string and the second parameter is the object. The format() method returns the string “The number is: 0000013002”.
Example for String. format() method in Java to show the different formatting specifies –
Code:
package jex; import java.util.*; public class Ex { public static void main( String[] args ) { // Integer value String s1 = String.format( "The Integer number is : %d" , 132 ); // Float value String s2 = String.format( "The Float number is : %f" , 132.00 ); // Hexadecimal value String s3 = String.format( "The Hexadecimal number is : %x" , 132 ); // Char value String s4 = String.format( "The Char number is : %c" , 'a'); // String value String s5 = String.format( "The String number is : %s" , "Hello world" ); System.out.println( s1 ); System.out.println( s2 ); System.out.println( s3 ); System.out.println( s4 ); System.out.println( s5 ); } }
An output of the above code is –
As in the above program, the String. format() method is used to creates the formatted string. In the String.format() method used the different format specifies for different data types like %d (integer), %f (float), %x (Hexadecimal), %c (character), and %s (string). Next, printing the formatted string, as we can see in the above output.
Example for String. format() method in Java to show the formatting specifier with the different width –
Code:
package jex; import java.util.*; public class Ex { public static void main( String[] args ) { // Filling with zeroes String s1 = String.format( "*%011d*" , 101 ); // Left-justifying within the specified width String s2 = String.format( "*%-11d*" , 101 ); String s3 = String.format( "*% d*" , 101 ); // Specifying length of integer String s4 = String.format( "*%11d*" , 101 ); System.out.println( s1 ); System.out.println( s2 ); System.out.println( s3 ); System.out.println( s4 ); } }
An output of the above code is –
As in the above program, the String. format() method is used to creates the formatted string. The String. format() method used the different widths for integer format Specifier. Next, printing the different formatted strings, as we can see in the above output.
Example for String. format() method in Java to show the specifying argument positions –
Code:
package jex; import java.util.*; public class Ex { public static void main( String[] args ) { String str1 = "Hello World"; int no = 100; // Specifying argument positions. The %1$ is for the first argument and the %2$ is for the second argument. String str2 = String.format( "The String is : %1$s and %1$s. \n And the number is : %2$s" , str1, no ); System.out.println( str2 ); } }
An output of the above code is –
As in the above program, the String. format() method is used to creates the formatted string. The String. format() method used the argument positions for the string and the integer format Specifier. The %1$ specifies the first argument, the %2$ specifies the second argument and so all. Next, printing the different formatted strings, as we can see in the above output.
The java string format() method returns a formatted string based on the locale, format, and arguments passed to it. The String.format() in Java is equivalent of the sprintf().The String. format method can be used to assign or store a formatted String to another String.
The above is the detailed content of sprintf Java. For more information, please follow other related articles on the PHP Chinese website!