w3resource

Java String: format() Method

format() Method

Contents:

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

The format() method is used to get a formatted string using the specified format string and arguments.

args - Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The number of arguments is variable and may be zero. The maximum number of arguments is limited by the maximum dimension of a Java array as defined by

Java Platform: Java SE 8

Syntax:

format(String format, Object... args)

Parameters:

Name Description Type
format A format string. String

Return Value: A formatted string.

Return Value Type: String

Throws:
IllegalFormatException - If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments are given the format string, or other illegal conditions.

Example: Java String format() Method

The following example shows the usage of java String() method.

importjava.util.Calendar;

public class Example {

public static void main(String[] args) { 
    Calendar c = Calendar.getInstance();
System.out.println("Current Date and Time :"); 
System.out.format("%tB %te, %tY%n", c, c, c);
System.out.format("%tl:%tM %tp%n", c, c, c); 
   }
   }
   
   

Output:

September 8, 2016                                                         
11:43 am

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

Returns a formatted string using the specified locale, format string, and arguments.

args - Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The number of arguments is variable and may be zero. The maximum number of arguments is limited by the maximum dimension of a Java array as defined by The Java™ Virtual Machine Specification. The behavior on a null argument depends on the conversion.

Java Platform: Java SE 8

Syntax :

format(Locale l, String format, Object... args)

Parameters:

Name Description Type
l The locale to apply during formatting. If l is null then no localization is applied. String
format A format string. String

Return Value: A formatted string.

Return Value Type: String

Throws:
IllegalFormatException - If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments are given the format string, or other illegal conditions.

Example: Java String format() Method

The following example shows the usage of java String() method.

import java.util.Formatter;
import java.util.Locale;

public class Example {

   public static void main(String[] args) {

      StringBuffer buffer = new StringBuffer();
      Formatter formatter = new Formatter(buffer, Locale.US);

      // format a new string
      String name = "to w3resource.com";
      formatter.format(Locale.US,"Welcome %s !", name);
      System.out.println();
      // print the formatted string with specified locale
      System.out.println(formatter + " " + formatter.locale());
      System.out.println();
   }
}

Output:

Welcome to w3resource.com ! en_US

Java Code Editor:

Previous:equalsIgnoreCase Method
Next:getBytes Method



Follow us on Facebook and Twitter for latest update.