Java String format()

Share this blog with others!

The Java string format() method returns a formatted string based on the locale, format, and arguments specified. This method can concatenate strings and format the resulting concatenated string.

The format() method in Java is similar to the sprintf() function in C and the Printf() function in Golang.

public class Blog {
    public static void main(String[] args) {
        String str = "Hack The Developer";

        // format string
        String formatStr = String.format("Hello: %s", str);

        System.out.println(formatStr);
    }
}

String format() Syntax

The syntax of the Java String format() method is:

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

Parameters

  • The format of the output string.
  • args specifying the number of arguments for the format string. It may be zero or more.

String format() Example:

Here is the Java String format() example:

public class Blog {
    public static void main(String[] args) {
        String str = "Hack The Developer";

        // format string
        String sf1 = String.format("Hello: %s", str);

        String sf2 = String.format("value is %f", 17.452342);
        String sf3 = String.format("value is %32.12f", 17.452342);

        System.out.println(sf1);
        System.out.println(sf2);
        System.out.println(sf3);
    }
}

Output:

Hello: Hack The Developer
value is 32.334340
value is                  32.334340000000

Format Specifiers

The following are some of the most prevalent format specifiers for the String format() function:

Format SpecifierData TypeDescription
%b, %BAny“true” if non-null, “false” if null
%s, %SStringString value
%c, %CcharA Unicode character
%dinteger (byte, short, int, long, bigint)Decimal Integer
%ointeger (byte, short, int, long, bigint)Octal Integer
%x, %Xinteger (byte, short, int, long, bigint)Hexadecimal Integer
%e, %EFloatA decimal number in scientific notation
%fFloatDecimal number
%nNonePlatform-specific line separator
%tDate/TimeDate/Time value
%hAnyHex String of value from hashCode() method
List of Java String format specifiers

Example:

String str1 = String.format("%d", 100);          // Integer value  
String str2 = String.format("%s", "HTD");        // String value  
String str3 = String.format("%f", 100.00);       // Float value  
String str4 = String.format("%x", 100);          // Hexadecimal value  
String str5 = String.format("%c", 'd');          // Char value  

Java String format() with Locale

If you need to operate with the supplied locale, the String format() method has a different syntax.

Syntax:

public static String format(Locale loc, String form, Object... args)

Example:


import java.util.Locale;

public class Blog {
    public static void main(String[] args) {
        int number = 20051238;

        // using the current locale
        String result = String.format("Number: %,d", number);

        System.out.println(result);

        // GERMAN locale
        String germanResult = String.format(Locale.GERMAN, "Number in German: %,d", number);

        // US Locale
        String usResult = String.format(Locale.US, "Number in US: %,d", number);

        // FRENCH locale
        String frenchResult = String.format(Locale.FRENCH, "Number in French: %,d", number);

        // INDIAN locale
        String indianResult = String.format(Locale.forLanguageTag("en-IN"), "Number in Indian: %,d", number);

        System.out.println(germanResult);
        System.out.println(usResult);
        System.out.println(frenchResult);
        System.out.println(indianResult);
    }
}

Output:

Number: 20,051,238
Number in German: 20.051.238
Number in US: 20,051,238
Number in French: 20?051?238
Number in Indian: 20,051,238

Learn about Java Virtual Machine.

0 0 votes
Article Rating

Share this blog with others!
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Scroll to Top