It is a method that formats and returns a character string based on the format specified in the argument.
The first argument must be written according to the specified format. After the second argument, pass the variable to be assigned to the format decided by the first argument.
public static String format(String format,Object... args)
Returns a formatted string with the specified formatted string and arguments.
** Specify the format by writing (% ~) **.
** Use d
to format integers (decimal integers) and s
** to format strings.
int year = 2021;
String str = String.format("This year%It's d years.", year);
System.out.println(str); //This year is 2021.
//Add 0 to the beginning
System.out.println(String.format("%05d", 1000)); //01000
System.out.println(String.format("%06d", 1000)); //001000
//Add characters
System.out.println(String.format("%d and%d", 1000,2000)); //1000 and 2000
//Format string
System.out.println(String.format("%s/%s/%s", "2021","01","03")); //2021/01/03
Recommended Posts