Java String format() Method

Java String Format Method

What is String?

In Java, an string class is a series of characters that are interpreted literally by a script. An object called a string represents a string of characters. Java.lang is used. A string object is made using the String class.

  • Numerous methods, including compare(), concat(), equals(), split(), length(), replace(), compareTo(), intern(), and substring(), are available in the Java String class.

To know more about String Class in java read the complete article.

String Format() Method:

The format() method of a java string returns a formatted string with the specified locale, format, and parameters.
The formatting string creates a String by formatting the supplied inputs. An argument index, flags, width definition, precision modifier, and conversion characters are all included in a formatting string.

  • The java.lang.String class provides a lot of built-in methods that are used to manipulate string in Java.These methods help us to perform operations on String objects such as trimming, concatenating, converting, comparing, replacing strings etc.

Format(), an equivalent class method for the String class, returns a String object as opposed to a PrintStream object.

When the locale is left blank in the String.format() method, the default locale is used by calling the Locale.getDefault() method.

Syntax:

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

Parameters:

format - A format string
args - Arguments referenced by the format specifiers in the format string.

Let’s look at a string-related Java program where the Java String Format() method is used to perform an operation on the given string.

Examples : Format()ing two given strings difference

Run
public class Main{  
   public static void main(String args[]){  
	String str = "Hello, PrepInsta User";  
	String formattedString = String.format("My String is %s", str);  
	String formattedString2 = String.format("My String is %.6f",14.140);
	System.out.println(formattedString); 
	System.out.println(formattedString2);  
   }
}

Output

My String is Hello, PrepInsta User
My String is 14.140000

Examples 2 : Java String Concat() Method

Run
public class Main{  
   public static void main(String args[]){  
	String str1 = "What is PrepInsta";
	String str2 = "PrepInsta Prime";
	String fstr = String.format("My String is: %1$s, %1$s and %2$s", str1, str2);
	System.out.println(fstr);
   }
}

Output

My String is: What is PrepInsta, What is PrepInsta and PrepInsta Prime

Examples 3: Concatenating two given strings ignoring lower and upper case.

Run
public class Main{  
   public static void main(String args[]){  
	String str1 = "PrepInsta is best";
	String str2 = "Prime";
	String fstr = String.format("My String is: %1$s, %1$s and %2$s", str1, str2);
	System.out.println(fstr);
   }
}

Output

My String is: PrepInsta is best, PrepInsta is best and Prime

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription