void printf(String format, Object... args)A convenience method to write a formatted string to this output stream using the specified format string and arguments.中文注解:一个便捷的方法,使用指定的格式字符串和传入的参数,将格式化的字符串输出到输出流(输出设备)传入的参数 String format 字符串类...
String.format(“%10s, world”, “Hello”); // 输出 ” Hello, world” String.format(“%8d”, 123); // 输出 ” 123″ // 补齐空格并左对齐: String.format(“%-10s, world”, “Hello”); // 输出 “Hello , world” String.format(“%-8d”, 123); // 输出 “123 “ // 补齐 0 ...
importjava.util.Scanner;publicclassMain{publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);System.out.print("请输入字符串1: ");Stringstr1=scanner.nextLine();StringformattedString=String.format("%-20s",str1);System.out.print("请输入字符串2: ");Stringstr2=scanner.nextLine(...
217.58)"// Writes a formatted string to System.out.System.out.format("Local time: %tT", Calendar.getInstance());// -> "Local time: 13:34:18"// Writes formatted output to System.err.System.err.printf("Unable to open file '%1$s': %2$s",...
intnum=123;StringformattedString=String.format("整数:%d",num);System.out.println(formattedString); 1. 2. 3. 输出: 整数:123 1. String.format()方法可以在不直接输出的情况下生成格式化的字符串,我们可以将其用于写入文件或其他操作。 5. 使用PrintWriter类输出整数到文件 ...
In the example, we print values in English and French locales. $ java Main.java 12568120.214000 12568120,214000 Monday lundi Source Java String - language reference In this article we have formatted strings in Java. Author My name is Jan Bodnar, and I am a passionate programmer with extensive...
To print data to the file, we have used the print() method. Here, when we run the program, the output.txt file is filled with the following content. This is a text inside the file. printf() Method The printf() method can be used to print the formatted string. It includes 2 para...
System.out.print(String.format("|%d|", 43)); The result: |43| The same logic can be applied to string also. If you wish to specify no. of characters to show: System.out.print(String.format("%.4s", "Hola Amigo!")); The result: ...
This will print the string as it is. System.out.printf(“%12s”,Str1); Here the field width is 12 characters. As Str1 has only 10 characters it will have 2 leading spaces. System.out.printf(“%-12sf”,Str1); This is similar to the previous example. The difference is it will hav...
public static void print() { formatter.format("%-15s %5d %10.2f\n", "My name is huhx", 5, 4.2); formatter.format("%-15.4s %5d %10.2f\n", "My name is huhx", 5, 4.1); } public static void main(String[] args) {