Fundamentals 6 min read

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.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Master Java String Formatting: 9 Practical Techniques with Code Samples

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.

<code>String name = "pack";    
int age = 33;    
String ret = String.format("我是 %s, 今年 %d 岁", name, age); // Output: 我是 pack, 今年 33 岁
System.err.println(ret);</code>
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.

<code>String name = "pack";    
int age = 33;    
String ret = MessageFormat.format("我是 {0}, 今年 {1} 岁", name, age); // Output: 我是 pack, 今年 33 岁
System.err.println(ret);</code>

3. Using text blocks for long strings

Java 15+ supports text blocks ("""…""") for multi‑line literals, and the formatted() method for concise formatting.

<code>String name = "pack";    
int age = 33;    
String ret = "我是 %s, 今年 %d 岁".formatted(name, age); // Output: 我是 pack, 今年 33 岁
System.err.println(ret);</code>

4. Adding commas in numbers

The %,.d specifier inserts grouping commas.

<code>int money = 4567890;    
String ret = String.format("%,d", money); // Output: 4,567,890
System.err.println(ret);</code>

5. Rounding numbers

Use %.2f to round to two decimal places.

<code>double pi = 3.1455926D;    
String ret = String.format("%.2f", pi); // Output: 3.15
System.err.println(ret);</code>

6. Zero‑padding numbers

Use %0<width>d to pad with leading zeros.

<code>int sno = 9527;    
String ret = String.format("%07d", sno); // Output: 0009527
System.err.println(ret);</code>

7. Using printf for formatted output

printf follows the same format rules as String.format.

<code>int sno = 6;    
System.err.printf("我的编号是:%06d", sno); // Output: 我的编号是:000006</code>

8. Adding whitespace

Use %10s or %-10s to add spaces before or after a string.

<code>String str1 = String.format("%10s", "Pack"); // Output: "      Pack"
String str2 = String.format("%-10s", "Pack"); // Output: "Pack      "
System.err.println(str1);
System.err.println(str2);</code>

9. Concatenating strings

Use %s to join multiple strings.

<code>String str1 = "join";    
String str2 = "pack";    
String ret = String.format("%s-%s", str1, str2); // Output: join-pack
System.err.println(ret);</code>

These techniques cover the most common scenarios for formatting strings, numbers, and dates in Java, helping developers produce clean and readable output.

JavaString FormattingprintfText BlocksMessageFormatString.format
Spring Full-Stack Practical Cases
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.