Best article on Java String Format Methods with an example by Codzify.com mentors

Manish Methani
4 min readOct 15, 2021

10 min 38 sec

Author: Manish Methani

Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects. The Java platform provides the String class to create and manipulate strings.

Topics which we are going to cover in this article today are

  1. How to create a string?
  2. Creating Format String
  3. String Format method example
  4. Date and Time Formatting
  5. Argument Index in Java
  6. Format Integer in java
  7. Specifying a width to format an integer
  8. Padding with zeros to format an integer
  9. String and character conversion methods
  10. Left justify the text to format an integer

How to create a string?

The most common way to create a string in Java is

String greeting = "Hello world!"

In this case, “hello world” is a string literal also known as a sequence of characters enclosed in double-quotes. Whenever string literals are encountered, a compiler creates a String object with its value.

Creating Format Strings

The string class provides a way to format the strings using the string format() method. The string format() method returns a String object rather than a PrintStream object.

The string format() method allows you to create a formatted string that you can reuse, as opposed to a one-time print statement. For example instead of

System.out.printf("The value of float is %f" +
“The value of integer is %d”, floatVar, Intvar );
String fs;
fs = String.format(“The value of float is %f” + “The value of integer is %d”, floatVar, Intvar System.out.println(fs);

String format method example

In the given java program, StringBuilder class is created along with Formatter class with the StringBuilder reference. In the next statement, we then used the format() method of formatter to create a formatted string. In the end, using the toString() method of StringBuilder we printed the output.

import java.util.*;
import java.lang.*;
import java.io.*;
class StringFormatMethod
{
public static void main (String[] args) throws java.lang.Exception
{
StringBuilder sbuf = new StringBuilder();
Formatter fmt = new Formatter(sbuf);
fmt.format("PI = %f%n", Math.PI);
System.out.print(sbuf.toString());
}
}

Argument Index in Java

Argument indices allow programmers to reorder the output. Let us see an example.

import java.util.*;
import java.lang.*;
import java.io.*;
class ArgumentIndexInjava
{
public static void main (String[] args) throws java.lang.Exception
{
System.out.printf("Before reordering = %s %s %s %s %s %s ", "1", "2", "3", "4", "5", "6" );
System.out.printf("After reordering = %6$s %5$s %4$s %3$s %2$s %1$s ","1", "2", "3", "4", "5", "6" );
System.out.printf("Before reordering = %d %d %d ", 101, 201, 301);
System.out.printf("After reordering = %2$d %3$d %1$d ", 101, 201, 301);
}
}

Output

Before reordering = 1 2 3 4 5 6
After reordering = 6 5 4 3 2 1
Before reordering = 101 201 301
After reordering = 201 301 101

Format Integer in Java

To format integer in Java, usually %d format specifier is used. You can use %d format specifier for all the integer datatypes like byte, short, int, long, BigInteger.

Default Formatting

import java.util.*;
import java.lang.*;
import java.io.*;
class Codzify
{
public static void main (String[] args) throws java.lang.Exception
{
System.out.println(String.format("%d", 50));
}
}

Output

50

Specifying a width

String.format("|%20d|", 50);import java.util.*;
import java.lang.*;
import java.io.*;
class Codzify
{
public static void main (String[] args) throws java.lang.Exception
{
System.out.println(String.format("|%10d|", 50));
}
}

Output

| 50|

Left justify with given width

String.format("|%-10d|", 50)import java.util.*;
import java.lang.*;
import java.io.*;
class Codzify
{
public static void main (String[] args) throws java.lang.Exception
{
System.out.println(String.format("|%-10d|", 50));
}
}

Output

|50 |

Padding with zeros

String.format("|%010d|", 50);import java.util.*;
import java.lang.*;
import java.io.*;
class Codzify
{
public static void main (String[] args) throws java.lang.Exception
{
System.out.println(String.format("|%010d|", 50));
}
}

Output

|0000000050|

String and Character Conversion

By default, strings can be printed using %s format specifier. Have a look at the given java program, we have printed a string “Hello World” using %s format specifier.

import java.util.*;
import java.lang.*;
import java.io.*;
class StringToCharacterConversionExample
{
public static void main (String[] args) throws java.lang.Exception
{
System.out.println(String.format("|%s|", "Hello World"));
}
}

Output

|Hello World|

After specifying a width,

import java.util.*;
import java.lang.*;
import java.io.*;
class Codzify
{
public static void main (String[] args) throws java.lang.Exception
{
System.out.println(String.format("|%20s|", "Hello World"));
}
}

Output

| Hello World|

Left Justify the text

import java.util.*;
import java.lang.*;
import java.io.*;
class Codzify
{
public static void main (String[] args) throws java.lang.Exception
{
System.out.println(String.format("|%-20s|", "Hello World"));
}
}

Output

|Hello World |

Print only given number of characters,

import java.util.*;
import java.lang.*;
import java.io.*;
class Codzify
{
public static void main (String[] args) throws java.lang.Exception
{
System.out.println(String.format("|%.5s|", "Hello World"));
}
}

Output

|Hello|

With given width and number of characters,

import java.util.*;
import java.lang.*;
import java.io.*;
class Codzify
{
public static void main (String[] args) throws java.lang.Exception
{
System.out.println(String.format("|%20.5s|", "Hello World"));
}
}

Output

| Hello|

I hope you got a better understanding of how do string formatting in java works. Check out our related articles section to find more interesting articles to read. Keep coding :)

You may also like to
Read about

1) What is an abstract class in Java?

2) How to use final keyword in Java?

3) What is an interface in Java?

4) What is an arraylist in Java with Practical example?

--

--

Manish Methani

Entrepreneur | Founder & CEO at Codzify | Passionate Coder 👨‍💻 | Built Codzify.com, CodePad (An Online Compiler) from Scratch.