Master Java String Formatting: 9 Practical Techniques with Code Samples
This article introduces a continuously updated Spring Boot 3 case collection and provides a step‑by‑step guide to nine Java string‑formatting methods—including String.format, MessageFormat, text blocks, number grouping, rounding, zero‑padding, printf, whitespace, and concatenation—complete with runnable code examples.
We are releasing a Spring Boot 3 practical case collection that now includes over 100 real‑world examples, with a promise of permanent updates and free source code for subscribers.
The collection is available as a PDF e‑book, and subscribers receive all accompanying Markdown notes and source projects.
1. Using format specifiers with String.format()
String#format() allows placeholders such as %s and %d to insert variables.
String name = "pack";
int age = 33;
String ret = String.format("我是 %s, 今年 %d 岁", name, age); // Output: 我是 pack, 今年 33 岁
System.err.println(ret);https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/util/Formatter.html#syntax
2. Using MessageFormat for global variables
MessageFormat is useful for handling locale‑aware messages.
String name = "pack";
int age = 33;
String ret = MessageFormat.format("我是 {0}, 今年 {1} 岁", name, age); // Output: 我是 pack, 今年 33 岁
System.err.println(ret);3. Using text blocks for long strings
Java 15+ supports text blocks ("""…""") for multi‑line literals, and the formatted() method for concise formatting.
String name = "pack";
int age = 33;
String ret = "我是 %s, 今年 %d 岁".formatted(name, age); // Output: 我是 pack, 今年 33 岁
System.err.println(ret);4. Adding commas in numbers
The %,.d specifier inserts grouping commas.
int money = 4567890;
String ret = String.format("%,d", money); // Output: 4,567,890
System.err.println(ret);5. Rounding numbers
Use %.2f to round to two decimal places.
double pi = 3.1455926D;
String ret = String.format("%.2f", pi); // Output: 3.15
System.err.println(ret);6. Zero‑padding numbers
Use %0<width>d to pad with leading zeros.
int sno = 9527;
String ret = String.format("%07d", sno); // Output: 0009527
System.err.println(ret);7. Using printf for formatted output
printf follows the same format rules as String.format.
int sno = 6;
System.err.printf("我的编号是:%06d", sno); // Output: 我的编号是:0000068. Adding whitespace
Use %10s or %-10s to add spaces before or after a string.
String str1 = String.format("%10s", "Pack"); // Output: " Pack"
String str2 = String.format("%-10s", "Pack"); // Output: "Pack "
System.err.println(str1);
System.err.println(str2);9. Concatenating strings
Use %s to join multiple strings.
String str1 = "join";
String str2 = "pack";
String ret = String.format("%s-%s", str1, str2); // Output: join-pack
System.err.println(ret);These techniques cover the most common scenarios for formatting strings, numbers, and dates in Java, helping developers produce clean and readable output.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
