×

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

String Coding Interview Questions in Java

What is String in Java?In Java, a String is a Class that is defined in the java.lang package. It isn't a basic data type like int or long. Character Strings...

5 minutes read.

JDBC vs ODBC

Difference Between JDBC and ODBC ODBC: ODBC (Open Database Connectivity) is the accepted method for accessing databases among organisations and programmers. A database is linked to other programmes, such as word processors, spreadsheets,...

4 minutes read.

MessageDigest in Java

The compression of mathematical numbers is accomplished using hash functions. In almost every data security application, hash functions are employed. The hashing, often called has values, returns a value called...

3 minutes read.

Relatively Prime in Java

In this article, you will be very well equipped with a knowledge of a relatively prime number and also Java programs to determine whether a given integer is a relatively...

4 minutes read.

Differences between Byte Code and Machine Code

This tutorial will discuss the differences between Byte Code and Machine Code. Before that, let's try to know what byte and machine code is. Introduction Every computer has a set of instructions...

4 minutes read.

Stack Program in Java

Stack Program in Java The Stack class is part of the collection framework that inherits the Vector class. Thus, the Stack class can also be called a subclass of the Vector...

5 minutes read.

Internal Working of ArrayList in Java

Java's version of a resizable array is called ArrayList. The dynamic growth of an array list ensures that there is always room for new elements. ArrayList's backing data structure is...

8 minutes read.

Java instance variable

An instance in object-oriented programming (OOP) is a particular implementation of any object. Each realized version of an item, which might vary in several ways, is an instance. Instantiation is...

3 minutes read.

Difference between this and super in Java

In this article, you will be very well equipped with the knowledge of this keyword and super keyword in java. You will be able to understand where to implement this...

3 minutes read.

Java Primitive Data Types

Primitive data types are the simplest data types in a programming language. They’re predefined in the language. The names of the primitive types are quite descriptive of the values that...

2 minutes read.

Java Read File Line by line

Java provides two options to read files line by line. Each option offers different benefits and has its own set of drawbacks. Following are the ways through which on accomplish...

5 minutes read.

How to Split the String in Java with Delimiter

In Java, splitting strings is a significant and typically used activity while coding. Java gives different ways of dividing the String. The most widely recognized way is to use the...

3 minutes read.

Display Unique Rows in a Binary Matrix in Java

To solve this problem, we must first locate and display the distinct rows of a supplied binary matrix afterward. We will go through how to show distinct rows inside a...

12 minutes read.

Concurrent Linked Deque in Java with Examples

Introduction Java's concurrent-linked deque, which holds its items as linked nodes, is unconstrained and thread-safe. Concurrent Linked Deque allows for element removal and addition on both sides because it implements the...

4 minutes read.

Empty Statement in Java

The three sorts of statements in Java are control, expression, and declaration statements. Additionally, another statement is referred to as an empty statement. In this section, we will discuss about...

4 minutes read.

Java Integer compare() method

The compare() method of Integer class compares the two specified int values. Syntax public static int compare(int x, int y) Parameters The parameters ‘x’ and ‘y’ represent the first and second int values to...

2 minutes read.

Instanceof operator in Java

To determine whether an object is an instance of the supplied type in Java, use the instanceof operator (class or subclass or interface). Because it compares the instance with type, the...

3 minutes read.

Java Boolean compareTo() method

The compareTo() method of Java Boolean class compares the Boolean argument with the Boolean instance and returns integer value, zero, or negative 1, or positive 1 based on the result...

2 minutes read.

Difference Between Thread.start() and Thread.run()

In the Java programming language, the multi-threading concept consists of the start() and run() methods. Thread.start(): The thread's execution is initiated by invoking the start() method. The start() method operates two threads...

4 minutes read.

Java Regular Expressions

Java Regular Expressions The Java Regex or Regular Expression is an API that defines a pattern for searching or manipulating strings. A regular expression is a pattern that can be as simple as...

8 minutes read.