Home >>Java String Methods >Java String format() method

Java String format() method

Java String format() method

Java format() method of java string returns the formatted string by provided locale, format, and arguments.

If you do not define the locale in the String.format() method, calling the Locale.getDefault() method would use the locale by default.

The java language format() method is like the sprint() function in c language, and the java language printf() method.

Internal implementation

public static String format(String format, Object... args) 
{  
       return new Formatter().format(format, args).toString();  
 }  

Signature

public static String format(String format, Object... args) 

Parameter

Locale - defines where the format() method is to be implemented.

Type - String format.

Args - Create string arguments. It may be zero, or more.

Returns

formatted string

String format() method example 1

public class FormatExample
{  
public static void main(String args[])
{  
String name="Pinky";  
String uf1=String.format("name is %s",name);  
String uf2=String.format("value is %f",12.653);  
String uf3=String.format("value is %12.65f",12.653);  
System.out.println(uf1);  
System.out.println(uf2);  
System.out.println(uf3);  
}
}  

Output
name is Pinky
value is 12.653000
value is 12.65300000000000000000000000000000000000000000000000000000000000000
String format Method Example 2

public class FormatExample2 
{  
    public static void main(String[] args) 
	{  
        String strln1 = String.format("%d", 50);           
        String strln2 = String.format("%s", "Sujata Singh");   
        String strln3 = String.format("%f", 50.87);        
        String strln4 = String.format("%x", 50);   
        String strln5 = String.format("%c", 's');           
        System.out.println(strln1);  
        System.out.println(strln2);  
        System.out.println(strln3);  
        System.out.println(strln4);  
        System.out.println(strln5);  
    }  
  
}

Output
50
Sujata Singh
50.870000
32
s

Java String Format Specifiers

Format Specifiers Data Type Output
%a floating point (except BigDecimal) Returns the Hex output of the number of floating points.
%b Any type "true" if non-null, "false" if null
%c Character Unicode character
%d integer (incl. int, long, byte, short, bigint) Decimal Integer
%e floating point In the scientific notation decimal number
%f floating point decimal number
%g floating point Decimal number, probably in scientific notation according to the precision and the value.
%h any type Hex Value string from hashCode() method.
%n None Platform-specific line separator.
%o integer (incl. int, long, byte, short, bigint) Octal number
%s any type String value
%t Date/Time (incl. Calendar, Date and TemporalAccessor, long) The Date / Time conversion prefix is percent t. After this more flags of formatting are required. See below the conversion date / time.
%x integer (incl. int, long, byte, short, bigint) Hex string.

No Sidebar ads