×

Java String format() method

format() method returns a formatted String based on the given locale,specified format and arguments.

Syntax:

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

Parameter:

locale : It specifies locale value to be applied on the format() method.

format: It specifies the format of the output String

args : It specifies the number of arguments for the format String.It may be zero or more

Returns:

It returns a formatted String

Java String format() Example 1

public class JavaStringFormatEx1 {
    public static void main(String args[])
    {
        String s1 = "Tutorial And Example";
        // Concatenation of two strings
        String str1 = String.format("Welcome to %s", s1);
        // Output is given upto 11 decimal places
        String str2 = String.format("value is %.11f", 75.23456);
        System.out.println(str1);
        System.out.println(str2);
    }
}

Output:

Welcome to Tutorial And Example
value is 75.23456000000
value is 47.65734000

Java String format() Example 2

public class JavaStringFormatEx2
{ 
public static void main(String args[])
{ 
String s1="Nitish"; 
String f1=String.format("name is %s",s1); 
String f2=String.format("value is %f",54.15789); 
String f3=String.format("value is %54.15f",54.15789);
System.out.println(f1); 
System.out.println(f2); 
System.out.println(f3); 
  }
}

Output:

name is Nitish
value is 54.157890
value is 54.157890000000000

Java String format() Example 3

public class JavaStringFormatEx3 { 
    public static void main(String[] args) { 
        String s1 = String.format("%d", 101);          // Integer value
        String s2 = String.format("%f", 101.00);       // Float value
        String s3 = String.format("%x", 101);          // Hexadecimal value
        String s4 = String.format("%c", 'N');          // Char value 
        String s5 = String.format("%s", "Roy Nitish Nk"); // String value 
        System.out.println(s1); 
        System.out.println(s2); 
        System.out.println(s3); 
        System.out.println(s4); 
        System.out.println(s5); 
    } 
}

Output:

101101.
00000065
N
Roy Nitish Nk

Java String format() Example 4

public class JavaStringFormatEx4 { 
    public static void main(String[] args) {         
        String str1 = String.format("%d", 101); 
        String str2 = String.format("|%10d|", 101);  // Specifying length of integer 
        String str3 = String.format("|%-10d|", 101); // Left-justifying within the specified width 
        String str4 = String.format("|% d|", 101);  
        String str5 = String.format("|%010d|", 101); // Filling with zeroes 
        System.out.println(str1); 
        System.out.println(str2); 
        System.out.println(str3); 
        System.out.println(str4); 
        System.out.println(str5); 
    } 
}

Output:

101
101
101
101
0000000101

Related Topics

Kong Java Client

Kong is an Organization Microservice Programming interface gateway. Kong gives an adaptable deliberation layer that safely oversees correspondence among clients and microservices by means of a Programming interface. Otherwise called...

6 minutes read.

String Handling Method in Java

What is a String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

Graphics Program in Java

Graphics Program in Java The graphics program in Java is part of the Swing program in Java. In this section, we will learn about the implementation of custom graphics using some...

6 minutes read.

Java Math floor() Method

The floor() method of Math class returns the largest double value that is equal to a mathematical integer and is less than or equal to the argument. Syntax: public static double floor(double...

2 minutes read.

Java.net.ConnectionException

java.net.ConnectException: Connection rejected: the interface is the most continuous sort of happening, organizing special cases in Java at whatever point the product is in client-server engineering and attempting to make...

3 minutes read.

Java Arrays Fill

We may use the Arrays.fill () function to fill a whole array or a subset of it. Arrays.fill () may fill both 2D and 3D arrays. Syntax: Arrays.fill(boolean[] fillArr, int fromIndex, int toIndex, boolean val )   Parameters: The array to be filled...

4 minutes read.

How to convert double to String in Java

How to Convert double to String in Java It is used when we want to convert double primitive to String type. There are two methods to convert double to String. Using String.valueOf()...

2 minutes read.

Lombok Java

What is Lombok java? A well-liked and widely-used Java framework that is used to reduce or eliminate boilerplate code is called Project Lombok. Both time and effort are saved. We may...

7 minutes read.

Java Speech Recognition

The customer experience and convenience are improved with its utilization. Command and control interest in buying and voice synthesizers are supported by the cross-platform API defined by the API. For...

4 minutes read.

How to find the length of an Array in Java

Arrays: An array is a sort of container object that stores constant quantities of values of a single type in one memory area. A finite number of items must all be...

3 minutes read.

Calculator Program in Java

Calculator Program in Java A calculator performs the basic mathematical operations such as addition, subtraction, multiplication, etc. In this section, we will create a calculator program in Java using switch case...

5 minutes read.

What are Array strings in Java?

In normal programming, An array is a group and a collection of identical forms of data that are stored in a sequential memory region and may be accessed using their...

4 minutes read.

Transaction Management in java

Definition: A database application is an application that is running against a relational database and executes one or more transactions. A transaction is an executing program that contains some database operations,...

4 minutes read.

Getting Synchronized Set from Java HashSet

The synchronizedSet() technique for java.util.Collections class is utilized to return a synchronized (string safe) set supported by the predetermined set. To ensure sequential access, it is important that everything admittance...

4 minutes read.

Java Program to find the smallest element in a tree

The variable min is used to store the data of the root, which is initially defined. The smallest node in the left subtree is then located by moving through the...

3 minutes read.

Java Boolean compare() method

The compare() method of Java Boolean class compares the specified Boolean values and returns a positive 1 or negative 1 or zero integer value based on the result. Syntax public static int...

2 minutes read.

Nth node from the end of the Linked list in Java

In talks with leading IT organizations like Google, Amazon, TCS, Accenture, etc., this extremely intriguing subject is constantly brought up. The goal of the problem-solving exercise is to evaluate the...

6 minutes read.

What is string in Java why it's immutable

String in Java Strings are a series of characters commonly used in Java language, which are considered objects in the Java. To create and manipulate strings, java will provide the String...

4 minutes read.

Static() Function in Java

The static keyword in Java is suitable for variables, constants, and functions. The static keyword is mainly used to control storage so that it may be appropriately used. We shall...

3 minutes read.

Java Queue

The Java.util package has the interface Queue, which does extend the Collection interface. It is used to protect the parts that are managed using the FIFO approach. Being an interface, the...

5 minutes read.