English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Array

Java Object-Oriented(I)

Java Object-Oriented(II)

Java Object-Oriented(III)

Java Exception Handling

Java List(List)

Java Queue(Queue)

Java Map Collection

Java Set Collection

Java Input Output (I)/O)

Java Reader/Writer

Java Other Topics

Java String format() Usage and Example

Java String (String) Methods

The Java String format() method returns a formatted string based on the parameters passed.

The syntax of the String format() method is:

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

Note:

  • format() is a static method. We use the class name String to call the format() method.

  • In the above code, ... indicates that multiple objects can be passed to format().

format() parameters

  • format - Format string

  • args - 0 or more parameters

format() return value

  • Returns the formatted string

Example1: Java String format()

class Main {
  public static void main(String[] args) {
    String language = "Java";
    int number = 30;
    String result;
    // Format an object as a string
    result = String.format("Language: %s", language);
    System.out.println(result); // Language: Java
    //Format the number as a hexadecimal number
    result = String.format("Hexadecimal number: %x", number); // 1e
    System.out.println(result); // Hexadecimal number: 1e
  }
}

In the above program, note the code

result = String.format("Language: %s", language);

This "Language: %s" is aFormat string.

Replace %s in the format string with the content of language. %s is a format specifier.

Similarly, %x is replaced with the hexadecimal value of number in String.format("Number: %x", number).

Format specifier

The following are commonly used format specifiers:

SpecifierDescription
%b, %BAccording to the parameter as "true" or "false"
%s, %SA string
%c, %CUnicode character
%dDecimal integer (only for integers)
%oOctal integer (only for integers)
%x, %XHexadecimal integer (only for integers)
%e, %EUsed for scientific notation (used for floating-point numbers)
%fUsed for decimal numbers (used for floating-point numbers)

Example2The string format of a number

class Main {
  public static void main(String[] args) {
    int n1 = 47;
    float n2 = 35.864f;
    double n3 = 44534345.76d;
    //Formatted as octal digits
    System.out.println(String.format("1In octal: %o", n1));  // 57
    //Formatted as hexadecimal digits
    System.out.println(String.format("1In hexadecimal: %x", n1));  // 2f
    System.out.println(String.format("1In hexadecimal: %X", n1));  // 2F
    //Formatted as a string
    System.out.println(String.format("1As a string: %s", n1));  // 47
    System.out.println(String.format("2As a string: %s", n2));  // 35.864
    //Scientific notation format
    System.out.println(String.format("3Scientific notation: %g", n3)); // 4.45343e+07
  }
}

Output Result

n1Octal: 57
n1Hexadecimal: 2f
n1Hexadecimal: 2F
n1As a string: 47
n2As a string: 35.864
n3Scientific notation: 4.45343e+07

You can use multiple format specifiers in the format string.

Example3: Using multiple format specifiers

//Using multiple format specifiers
//Format string
class Main {
  public static void main(String[] args) {
    int n1 = 47;
    String text = "Result";
    System.out.println(String.format("%s\nHexadecimal: %x", text, n1));
  }
}

Output Result

Result
Hexadecimal: 2f

Here, %%s is replaced with the value of text. Similarly, %%o is replaced with n1The hexadecimal value.

How Java String format() works

Example4: The format of decimal numbers

class Main {
  public static void main(String[] args) {
    float n1 = -452.534f;
    double n2 = -345.766d;
    //Format the floating-point number as is
    System.out.println(String.format("1 =%f", n1)); // -452.533997
    System.out.println(String.format("2 =%f", n2)); // -345.766000
    //Display to two decimal places
    System.out.println(String.format("1 =%.2f", n1)); // -452.53
    System.out.println(String.format("2 =%.2f", n2)); // -345.77
  }
}

Output Result

n1 = -452.533997
n2 = -345.766000
n1 = -452.53
n2 = -345.77

Note:When we use %f to format-452.534When-452.533997. This is not because the format() method. Java does not return the exact representation of floating-point numbers.

Use %%.2When the f format specifier is used, format() gives two digits after the decimal point.

Example5: Fill the number with spaces and 0

// using more than one format specifiers
// in a format string
class Main {
  public static void main(String[] args) {
    int n1 = 46, n2 = -46;
    String result;
        //Fill the number with spaces
        //The length of the string is5
    result = String.format("|%5d|", n1); // |   46|
    System.out.println(result);
        //Fill the number with digits 0
        //The length of the string is5
    result = String.format("|%5d|", n1); // |00046|
    System.out.println(result);
    //Use a sign before the number
    result = String.format("%+d", n1); // +46
    System.out.println(result);
    result = String.format("%+d", n2); // -46
    System.out.println(result);
        //Enclose negative numbers in parentheses
        //and remove the sign
    result = String.format("%(d", n2); // (46)
    System.out.println(result);
  }
}

Example6: Use 0x and 0 before hexadecimal and octal

//Use 0x before hexadecimal
//Use 0 before octal
class Main {
  public static void main(String[] args) {
    int n = 46;
    System.out.println(String.format("%#o", n)); // 056
    System.out.println(String.format("%#x", n)); // 0x2e
  }
}

Java String format() with a language environment

If a specific language environment must be used, the String format() method also has another syntax.

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

Example7: Use GERMAN locale in format()

//Use Locale
import java.util.Locale;
class Main {
  public static void main(String[] args) {
    int number = 8652145;
    String result;
    //Use the current locale
    result = String.format("Numbers: %,d", number);
    System.out.println(result);
    //Use GERMAN locale as the first parameter
    result = String.format(Locale.GERMAN, "German Numbers: %,d", number);
    System.out.println(result);
  }
}

Output Result

Numbers: 8,652,145
German Numbers: 8.652.145

Note:In Germany, integers are represented by a period instead of a comma.

Java String (String) Methods