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
Syntax of the String. format() method in Java
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:
- locale – This is not an optional parameter. It specifies the locale to be applied on the format() method
- format – This is not an optional parameter. It specifies the formatting to be applied on the string.
- args – This is an optional parameter. It specifies the parameters for the formatting string. It can be zero or more parameters.
- Return value – The return value of this function is the formatted string.
The implementation of the String.format() method in Java
public static String format(String format, Object... args) { return new Formatter().format( format, args ).toString( ); }
Working of the String. format() method in Java
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”.
Examples of sprintf Java
Example for String. format() method in Java to show the different formatting specifies –
Example #1
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 –
Example #2
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 –
Example #3
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.
Conclusion
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!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Mac version
God-level code editing software (SublimeText3)