How to use String format ( ) function in Java
In this tutorial, we will learn how to use the String format function in Java.
Let us go deep into what String formatting is. String formatting can be defined as formatting a String or altering its representation in the terminal. String format() function helps us to do the task very easily. There are many things that can be done using this function like:
- Concatenation of Strings.
- Format the output of the Concatenated String
So with the help of this tutorial, we will have an idea of how the String format() function works and what is its application.
There are in total two syntax or ways by which String format function is implemented:
- String format (String format, Object . . . args).
- String format (Locale l, String format, Object . . . args)
Both the functions return the formatted String. If Locale is not specified in the syntax then Java takes the Locale.getDefault(). Let’s discuss the parameter of the format function:
- String Format: Format of the String or representation
- Locale: specifies or indicates the locale to be applied in the format function.
- Object . . . args: arguments for the formatted String,it can be anything.
A Simple Example of String Format () Function
CODE:
class stringformat{ public static void main(String[] args) { String str="Subhojeet"; double x=12.3456; //Concatenating str with another String String concat=String.format("Hello CodeSpeedy My name is %s", str); //Formatting the double with its decimal places String deci=String.format("The formatted value is %.5f",x); System.out.println(concat); System.out.println(deci); } }
OUTPUT:
Hello CodeSpeedy My name is Subhojeet The formatted value is 12.34560
In this example %s is used for string concatenation. In here %s is a format Specifier for the string which returns a string value. The next specifier used is %f Which specifies and returns the formatted decimal values.
Method 2 to use the String format( )
In this method, we will convert an integer value to hexadecimal or octal. This method also supports datatypes and formats all to String types.
CODE:
class stringformat{ public static void main(String[] args) { int num=100; String integernum=String.format("Decimal value %d",num); // Decimal value of num String hexadecimalnum=String.format("Hexadecimal value %x",num); // Hexadecimal value of num String octalnum=String.format("Octal value %o",num);// Octal value of num String decimalhex=String.format("decimal point hex value %a",12.45); // float value to hex System.out.println(integernum); System.out.println(hexadecimalnum); System.out.println(octalnum); System.out.println(decimalhex); } }
OUTPUT:
Decimal value 100 Hexadecimal value 64 Octal value 144 decimal point hex value 0x1.8e66666666666p3
Method 3 to use String format() function
We can use %1$ %2$ . . . format specifiers to concat the string with writing arguments repeatedly.
CODE:
class stringformat{ public static void main(String[] args) { String str="Subhojeet"; String str2="Ghosh"; String formatString=String.format("My name is %1$s , %1$s %2$s . Mind it ",str,str2); // %1$s is Subhojeet and %2$s is Ghosh System.out.println(formatString); } }
OUTPUT:
My name is Subhojeet, Subhojeet Ghosh. Mind it
Some Java String format () Specifiers:
%d – Integer %c – Character %s – String %o – Octal %x – Hexadecimal %h – hash code of a value %f – Floating number
So these were some of the methods and applications of the String format function.
Hope you like my Content. Happy Coding !!!!
Leave a Reply