What is java string format() method?

The java string format() method returns a formatted string using the given locale, specified format string, and arguments. In simple words, java string format() method is used for formatting the string. You can do so many things by using the string format() method. Like you can concatenate the string using this method, and at the same time, you can format the output of concatenated string.

Syntax:

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

returns a formatted string 

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

Parameters:

Here are the parameters of java string format() method as below.

Locale: used to specifies the locale to be applied on the format() method.

format: Format of the string.

ages: the number of arguments for the format string and it may b zero or more.

Returns

The return method returns a formatted string.

Exception:

NullPointerException: If format is null.

IllegalFormatExcepion: If the format is illegal or there are insufficient arguments.

For Example:

Here is a Simple Example of the Java String format() method.

Class Mywork {
    public static void main(String agrs[])
    {
     String str = "LearnWithShikha.";

     //Concatenation string using format
        String Mywork = String.format("My Company name is %s", str);

     /*formatting the value passed and concatenation at the same          time
     /* %.6f is for having ^ digits in the fractional part */
      String str2 = String.format("My string is %.6f ", 13.910);


      System.out.orintIn(Mywork);
      System.out.printIn(str2);
    }
}

Output:

My Company name is learnWithShikha.
My string is 13.91000

Java String format() specifiers:

Here, we are providing a table of format specifiers supported by the Java String.

SpecifierData TypeOutput
%afloating point(except BigDecimal)Returns Hex output of floating point number.
%bAny type“true” if non-null, “false” if null
%ccharacterUnicode character
%dinteger(incl. byte, short, int, long, bigint)Decimal integer
%efloating pointdecimal number with scientific notation
%ffloating pointdecimal number
%gfloating pointdecimal number, possibly in scientific notation depending on the precision and value
%hany typeHex string of value from hashCode() method.
%nnoneplatform-specific line separator.
%ointeger(incl.byte, short, int, long, bigint)Octal number
%sany typeString value
%tData/Time(incl. long, Calender, Date and TemporalAccessor)%t is the prefix for Date/Time conversions. More formatting flags are needed after this. See Date/Time conversion below.
%xinteger (incl. byte, short, int, long, bigint)
Hex string.

Date and Time Formatting:

 FLAGNOTES
 %tAFull name of the day of the week, e.g. “Sunday“, “Monday
 %taAbbreviated name of the week day e.g. “Sun“, “Mon“, etc.
 %tBFull name of the month e.g. “January“, “February“, etc.
 %tbAbbreviated month name e.g. “Jan“, “Feb“, etc.
 %tCCentury part of year formatted with two digits e.g. “00” through “99”.
 %tcDate and time formatted with “%ta %tb %td %tT %tZ %tY” e.g. “Fri Feb 17 07:45:42 PST 2017
 %tDDate formatted as “%tm/%td/%ty
 %tdDay of the month formatted with two digits. e.g. “01” to “31“.
 %teDay of the month formatted without a leading 0 e.g. “1” to “31”.
%tFISO 8601 formatted date with “%tY-%tm-%td“.
%tHHour of the day for the 24-hour clock e.g. “00” to “23“.
%thSame as %tb.
%tIHour of the day for the 12-hour clock e.g. “01” – “12“.
%tjDay of the year formatted with leading 0s e.g. “001” to “366“.
%tkHour of the day for the 24 hour clock without a leading 0 e.g. “0” to “23“.
%tlHour of the day for the 12-hour click without a leading 0 e.g. “1” to “12“.
%tMMinute within the hour formatted a leading 0 e.g. “00” to “59“.
%tmMonth formatted with a leading 0 e.g. “01” to “12“.
%tNNanosecond formatted with 9 digits and leading 0s e.g. “000000000” to “999999999”.
%tpLocale specific “am” or “pm” marker.
%tQMilliseconds since epoch Jan 1 , 1970 00:00:00 UTC.
%tRTime formatted as 24-hours e.g. “%tH:%tM“.
%trTime formatted as 12-hours e.g. “%tI:%tM:%tS %Tp“.
%tSSeconds within the minute formatted with 2 digits e.g. “00” to “60”. “60” is required to support leap seconds.
%tsSeconds since the epoch Jan 1, 1970 00:00:00 UTC.
%tTTime formatted as 24-hours e.g. “%tH:%tM:%tS“.
%tYYear formatted with 4 digits e.g. “0000” to “9999“.
%tyYear formatted with 2 digits e.g. “00” to “99“.
%tZTime zone abbreviation. e.g. “UTC“, “PST“, etc.
%tzTime Zone Offset from GMT e.g. “-0800“.

Note: Using the formatting characters with “%T” instead of “%t” in the table below makes the output uppercase.

Recommended Posts:

Difference between the Java and JavaScript

What is JavaScript?

How are java objects stored in memory?

Compilation and Execution of a java program

Reference:

www.geeksforgreeks.com

www.dzone.com

Leave a Reply

Your email address will not be published. Required fields are marked *