Ask Your Question
3

What is the process of formatting strings in Java?

asked 2021-09-07 11:00:00 +0000

djk gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-01-03 20:00:00 +0000

lalupa gravatar image

The process of formatting strings in Java involves using the String.format() method or the System.out.printf() method. Both methods use format specifiers to denote where to insert values into the string.

Example of using String.format() method:

String name = "John";
int age = 30;
String message = String.format("My name is %s and I am %d years old.", name, age);
System.out.println(message);

Output: My name is John and I am 30 years old.

Example of using System.out.printf() method:

String name = "Mary";
int age = 25;
System.out.printf("My name is %s and I am %d years old.", name, age);

Output: My name is Mary and I am 25 years old.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-09-07 11:00:00 +0000

Seen: 12 times

Last updated: Jan 03 '23