Friday, January 10, 2025
HomeProgrammingJava String.format()

Java String.format()

The String.format() method in Java is used to create formatted strings, similar to printf(). It replaces placeholders in a format string with specified values.


Syntax:

java
String.format(String format, Object... args);

Example:

java
String name = "Alice";
int age = 25;
String result = String.format("Name: %s, Age: %d", name, age);
System.out.println(result);
// Output: Name: Alice, Age: 25

Common Placeholders:

  • %s – String
  • %d – Integer
  • %f – Floating-point number
  • %n – Newline
See also  Top 10 IDEs for Programmers

String.format() is ideal for building readable, dynamic text.

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x