String.format

Java Multithreading
Level 2 , Lesson 6
Available

"I also want to tell you about the String.format method."

"It's a static method of the String class, but it is very useful. Let me take a roundabout approach."

"If you need to display several variables on one line of text, how would you do it?"

"What text?"

"This, for example:"

Given the following variables:
String name = "Bender";
int age = 12;
String friend = "Fry";
int weight = 200;
Required output:
User = {name: Bender, age: 12 years, friend: Fry, weight: 200 kg.}

"I'd do it something like this:"

String name = "Bender";
int age = 12;
String friend = "Fry";
int weight = 200;

System.out.println("User = {name: " + name + ", age: " + age + " years, friend: " + friend + ", weight: " + weight + " kg.}");

"Not very readable, is it?"

"I think it's okay."

"But if you have long variable names or you need to call methods to get data, then it wouldn't be very readable:"

System.out.println("User = {name: " + user.getName() + ", age: " + user.getAge() + " years, friend: " + user.getFriends().get(0) + ", weight: " + user.getExtraInformation().getWeight() + " kg.}");

"Well, if that were the case, then, yes, it wouldn't be very readable."

"The fact is this happens all the time in real programs, so I want to show you how you can simplify your life with the String.format method."

"Please tell me quickly, what kind of magical method is this?"

"You can write the code above like this:"

String text = String.format("User = {name: %s, age: %d years, friend: %s, weight: %d kg.}",
user.getName(), user.getAge(), user.getFriends().get(0), user.getExtraInformation().getWeight())

System.out.println(text);

"The String.format method's first parameter is a format string that contains special characters (%s, %d) wherever we want to put values."

"After the format string, we pass the values that will replace the %s and %d characters."

"If we need to insert a string, then we write %s; if we need a number, then we use %d."

"It will be easier to see this in an example:"

Example
String s = String.format("a = %d, b = %d, c = %d", 1, 4, 3);
Result:
s will be equal to «a = 1, b = 4, c = 3»

"Yeah, that's very convenient."

"And you can also do it like this:"

Example
int a = -1, b = 4, c = 3;
String template;
 if (a < 0)
  template = "Warning! a = %d, b = %d, c = %d";
 else
  template = "a = %d, b = %d, c = %d";

System.out.println(String.format(template, a, b, c) );
Output:
Warning! a = -1, b = 4, c = 3

"Hmm. That is a really useful method. Thanks, Ellie."

"If you want to use other data types with the format method, here's a table that will help:"

Symbol Type
%s String
%d integers: int, long, etc.
%f real numbers: float, double
%b boolean
%c char
%t Date
%% Percent sign %

"Actually, these format specifiers also have their own settings and subsettings."

"But this is enough to get you started. If not, here's a link to the official documentation:"

Link to additional material

Comments (4)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Justin Smith Level 41, Greenfield, USA, United States
12 December 2021
When they tell you there is more at that last link, they mean it. What is introduced on this page is a tiny tip of the iceberg regarding String.format, and you'll need the other stuff for the following tasks.
Ahmad Level 23, Riyadh, Saudi Arabia
20 June 2020
in the first ex. Bender is 12 years old and weighted 200 kg. wow
Vahan Level 41, Tbilisi, Georgia
21 December 2019
I like this roundabout approach.
Krisztian Level 24, Budapest, Hungary
9 August 2019
Another Futurama reference! :D