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.
Returns A formatted string using the specified format string and arguments.
Note: If there are more arguments than format specifiers, the extra arguments are ignored. The number of arguments is variable and may be zero.
When the locale is left blank in the String.format() method, the default locale is used by calling the Locale.getDefault() method.
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
Explanation:Here the output comes is (My String is Hello, PrepInsta User
My String is 14.140000) because we are concatenating string using format and then *formatting the value passed and concatenating at the same time
* %.6f is for having 6 digits in the fractional part
*
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
Explanation:Here the output comes is ( My String is: What is PrepInsta, What is PrepInsta and PrepInsta Prime ) because we are specifying argument positions. %1$ is for the first argument and * %2$ is for the second argument.
Examples 3: Concatenating two given strings ignoring lower and upper case.
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
Explanation:Here the output comes is ( PrepInsta is best, PrepInsta is best and Prime ) because we are specifying argument positions. %1$ is for the first argument and * %2$ is for the second argument.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment