Home > Java > javaTutorial > body text

sprintf Java

王林
Release: 2024-08-30 15:18:36
Original
977 people have browsed it

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 Tests

Start 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
}
Copy after login

and

2.

public static String format(Locale locale, String format, Object... args)
{
// code
}
Copy after login

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( );
}
Copy after login

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 );
}
}
Copy after login

An output of the above code is –

sprintf Java

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 );
}
}
Copy after login

An output of the above code is –

sprintf Java

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 );
}
}
Copy after login

An output of the above code is –

sprintf Java

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!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template