๋ฌธ์ž์—ด.ํ˜•์‹

์ž๋ฐ” ๋ฉ€ํ‹ฐ์Šค๋ ˆ๋”ฉ
๋ ˆ๋ฒจ 2 , ๋ ˆ์Šจ 6
์‚ฌ์šฉ ๊ฐ€๋Šฅ

"๋˜ํ•œ String.format ๋ฐฉ๋ฒ• ์— ๋Œ€ํ•ด์„œ๋„ ๋ง์”€๋“œ๋ฆฌ๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค ."

" String ํด๋ž˜์Šค ์˜ ์ •์  ๋ฉ”์„œ๋“œ ์ด์ง€๋งŒ ๋งค์šฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์šฐํšŒ์ ์œผ๋กœ ์ ‘๊ทผํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค."

"ํ…์ŠคํŠธ ํ•œ ์ค„์— ์—ฌ๋Ÿฌ ๋ณ€์ˆ˜๋ฅผ ํ‘œ์‹œํ•ด์•ผ ํ•œ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?"

"๋ฌด์Šจ ๋ฌธ์ž?"

"์˜ˆ๋ฅผ ๋“ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค."

๋‹ค์Œ ๋ณ€์ˆ˜๊ฐ€ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.
String name = "Bender";
int age = 12;
String friend = "Fry";
int weight = 200;
ํ•„์š”ํ•œ ์ถœ๋ ฅ:
User = {name: Bender, age: 12 years, friend: Fry, weight: 200 kg.}

"๋‚˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค."

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.}");

"์ž˜ ์ฝํžˆ์ง€ ์•Š์Šต๋‹ˆ๊นŒ?"

"๊ดœ์ฐฎ์€ ๊ฒƒ ๊ฐ™์•„."

"ํ•˜์ง€๋งŒ ๋ณ€์ˆ˜ ์ด๋ฆ„์ด ๊ธธ๊ฑฐ๋‚˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•ด ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ ๊ฐ€๋…์„ฑ์ด ์ข‹์ง€ ์•Š์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค."

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

"๊ธ€์Ž„, ๊ทธ๋ ‡๋‹ค๋ฉด, ๊ทธ๋ž˜, ์ฝ๊ธฐ ์‰ฝ์ง€ ์•Š์„๊ฑฐ์•ผ."

"์‚ฌ์‹ค ์ด๊ฒƒ์€ ์‹ค์ œ ํ”„๋กœ๊ทธ๋žจ์—์„œ ํ•ญ์ƒ ๋ฐœ์ƒํ•˜๋ฏ€๋กœ String.format ๋ฐฉ๋ฒ•์œผ๋กœ ์‚ถ์„ ๋‹จ์ˆœํ™”ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ฃผ๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค."

"๋นจ๋ฆฌ ๋งํ•ด์ค˜, ์ด๊ฒŒ ๋ฌด์Šจ ๋งˆ๋ฒ•์˜ ์ˆ˜๋ฒ•์ด์•ผ?"

"์œ„ ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค."

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);

" String.format ๋ฉ”์„œ๋“œ์˜ ์ฒซ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ๊ฐ’์„ ๋„ฃ์„ ๋•Œ๋งˆ๋‹ค ํŠน์ˆ˜ ๋ฌธ์ž(%s, %d)๋ฅผ ํฌํ•จํ•˜๋Š” ํ˜•์‹ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค."

"ํ˜•์‹ ๋ฌธ์ž์—ด ๋‹ค์Œ์— %s ๋ฐ %d ๋ฌธ์ž๋ฅผ ๋Œ€์ฒดํ•  ๊ฐ’์„ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค."

"๋ฌธ์ž์—ด์„ ์‚ฝ์ž…ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ %s๋ฅผ ์“ฐ๊ณ , ์ˆซ์ž๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ %d๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค."

"์˜ˆ์ œ์—์„œ ์ด๊ฒƒ์„ ๋ณด๋Š” ๊ฒƒ์ด ๋” ์‰ฌ์šธ ๊ฒƒ์ž…๋‹ˆ๋‹ค:"

์˜ˆ
String s = String.format("a = %d, b = %d, c = %d", 1, 4, 3);
๊ฒฐ๊ณผ:
s๋Š” ยซa = 1, b = 4, c = 3ยป๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

"๋„ค, ์•„์ฃผ ํŽธ๋ฆฌํ•ฉ๋‹ˆ๋‹ค."

"๋˜ํ•œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค."

์˜ˆ
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) );
์‚ฐ์ถœ:
Warning! a = -1, b = 4, c = 3

"์Œ. ์ •๋ง ์œ ์šฉํ•œ ๋ฐฉ๋ฒ•์ด๊ตฐ. ๊ณ ๋งˆ์›Œ, ์—˜๋ฆฌ."

"ํฌ๋งท ๋ฐฉ๋ฒ•๊ณผ ํ•จ๊ป˜ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ ์œ ํ˜•์„ ์‚ฌ์šฉํ•˜๋ ค๋Š” ๊ฒฝ์šฐ ๋‹ค์Œ ํ‘œ๊ฐ€ ๋„์›€์ด ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค."

์ƒ์ง• ์œ ํ˜•
%์—์Šค ๋ˆ
%๋”” ์ •์ˆ˜: int, long ๋“ฑ
%์—ํ”„ ์‹ค์ˆ˜: float, double
%๋น„ ๋ถ€์šธ
%์”จ ์ˆฏ
%ํ‹ฐ ๋‚ ์งœ
%% ๋ฐฑ๋ถ„์œจ ๊ธฐํ˜ธ %

"์‹ค์ œ๋กœ ์ด๋Ÿฌํ•œ ํ˜•์‹ ์ง€์ •์ž์—๋Š” ๊ณ ์œ ํ•œ ์„ค์ •๊ณผ ํ•˜์œ„ ์„ค์ •๋„ ์žˆ์Šต๋‹ˆ๋‹ค."

"ํ•˜์ง€๋งŒ ์‹œ์ž‘ํ•˜๊ธฐ์— ์ถฉ๋ถ„ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ ๊ณต์‹ ๋ฌธ์„œ์— ๋Œ€ํ•œ ๋งํฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค."

์ถ”๊ฐ€ ์ž๋ฃŒ ๋งํฌ

์ฝ”๋ฉ˜ํŠธ
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION