String

String.format()

๋ฌธ์ž์—ด ํฌ๋งท์„ ์ œ๊ณตํ•˜๋Š” ๋ฉ”์„œ๋“œ

ํ•ด๋‹น ๋ถ€๋ถ„์— ๋งž๊ฒŒ ์›ํ•˜๋Š” ํฌ๋งท ํ˜•ํƒœ๋กœ ๋ณ€๊ฒฝ๋˜์–ด ๋ฐ”๋€Œ๊ฒŒ ๋œ๋‹ค.

  • %s : ์ŠคํŠธ๋ง
  • %d : ์ˆซ์ž
  • %f : ์‹ค์ˆ˜

Method Summary

public static String format (String format, Object... args) {
  /* statement */  
}

๋ฌธ์žํ˜•

ํ•ญ๋ชฉ ์„ค๋ช…
%s ํ•ด๋‹น ์œ„์น˜์— ๋ฌธ์ž์—ด์„ ๋Œ€์น˜ํ•œ๋‹ค.
%10s ์ˆซ์ž(10) ๋งŒํผ ๋ฌธ์ž์—ด ์ขŒ์ธก(+) ์— ๊ณต๋ฐฑ์„ ์ฑ„์›Œ ๋ฌธ์ž์—ด ๊ธธ์ด๋ฅผ ๋งŒ๋“ ๋‹ค
%-10s ์ˆซ์ž(10) ๋งŒํผ ๋ฌธ์ž์—ด ์šฐ์ธก(-) ์— ๊ณต๋ฐฑ์„ ์ฑ„์›Œ ๋ฌธ์ž์—ด ๊ธธ์ด๋ฅผ ๋งŒ๋“ ๋‹ค
%10.2s ์ (.) ์šฐ์ธก์— ํ•ด๋‹นํ•˜๋Š” ์ˆซ์ž(2) ๋งŒํผ ๋ฌธ์ž์—ด์„ ์ž๋ฅธํ›„ ์ (.) ์ขŒ์ธก ์ˆซ์ž(10)๋งŒํผ ๋ฌธ์ž์—ด ์šฐ์ธก์— ๊ณต๋ฐฑ ์„ ์ฑ„์›Œ ๋ฌธ์ž์—ด ๊ธธ์ด๋ฅผ ๋งŒ๋“ ๋‹ค
%-10.2s ์ (.) ์šฐ์ธก์— ํ•ด๋‹นํ•˜๋Š” ์ˆซ์ž(2) ๋งŒํผ ๋ฌธ์ž์—ด์„ ์ž๋ฅธํ›„ ์ (.) ์ขŒ์ธก ์ˆซ์ž(10)๋งŒํผ ๋ฌธ์ž์—ด ์ขŒ์ธก์— ๊ณต๋ฐฑ ์„ ์ฑ„์›Œ ๋ฌธ์ž์—ด ๊ธธ์ด๋ฅผ ๋งŒ๋“ ๋‹ค
String name = "one";

System.out.println(String.format("p1 : %s", name));
p1 : one

์ˆซ์žํ˜•

ํ•ญ๋ชฉ ์„ค๋ช…
%d ํ•ด๋‹น ์œ„์น˜์˜ ์ˆซ์ž๋กœ ๋Œ€์น˜ํ•œ๋‹ค.
%10d ์ˆซ์ž(10) ๋งŒํผ ์ขŒ์ธก(+)์— ๊ณต๋ฐฑ์„ ์ฑ„์›Œ ์ˆซ์ž์—ด๊ธธ์ด๋ฅผ ๋งŒ๋“ ๋‹ค
%-10d ์ˆซ์ž(10) ๋งŒํผ ์šฐ์ธก(-)์— ๊ณต๋ฐฑ์„ ์ฑ„์›Œ ์ˆซ์ž์—ด๊ธธ์ด๋ฅผ ๋งŒ๋“ ๋‹ค
%010d ์ˆซ์ž(10) ๋งŒํผ ์ขŒ์ธก(+)์— 0์„ ์ฑ„์›Œ ์ˆซ์ž์—ด๊ธธ์ด๋ฅผ ๋งŒ๋“ ๋‹ค
%,d ์ฒœ๋‹จ์œ„ ์ฝค๋งˆ(,) ์ฒ˜๋ฆฌ๋ฅผ ํ•œ๋‹ค.
int num = 102;

System.out.println(String.format("%04d", num));
0102

์‹ค์ˆ˜ํ˜•

ํ•ญ๋ชฉ ์„ค๋ช…
%f ํ•ด๋‹น ์œ„์น˜์˜ ์ˆซ์ž๋กœ ๋Œ€์น˜ํ•œ๋‹ค.
์ •์ˆ˜์ผ ๊ฒฝ์šฐ์—๋Š” ์ •์ˆ˜๋งŒ ๋‚˜์˜ค๋ฉฐ, ์‹ค์ˆ˜์ธ ๊ฒฝ์šฐ์—๋Š” ์†Œ์ˆ˜์  6์ž๋ฆฌ๊นŒ์ง€ ๋…ธ์ถœ๋œ๋‹ค.
%12f ์ˆซ์ž(12)๋งŒํผ ๋ฌธ์ž์—ด ์ขŒ์ธก(+)์— ๊ณต๋ฐฑ์„ ์ฑ„์›Œ ์†Œ์ˆ˜์  ์ดํ•˜ ์ž๋ฆฌ์ˆ˜๋ฅผ ํฌํ•จํ•˜์—ฌ ๊ธธ์ด๋ฅผ ๋งŒ๋“ ๋‹ค.
%-12f ์ˆซ์ž(12)๋งŒํผ ๋ฌธ์ž์—ด ์šฐ์ธก(-)์— ๊ณต๋ฐฑ์„ ์ฑ„์›Œ ์†Œ์ˆ˜์  ์ดํ•˜ ์ž๋ฆฌ์ˆ˜๋ฅผ ํฌํ•จํ•˜์—ฌ ๊ธธ์ด๋ฅผ ๋งŒ๋“ ๋‹ค.
%.2f ์ (.) ์šฐ์ธก์— ํ•ด๋‹นํ•˜๋Š” ์ˆซ์ž(2) ๋งŒํผ ์†Œ์ˆ˜์  ์ดํ•˜ ์ž๋ฆฌ๋ฅผ ๋ฐ˜์˜ฌ๋ฆผํ•˜์—ฌ ๊ธธ์ด๋ฅผ ๋งŒ๋“ ๋‹ค.
%010.2f ์ˆซ์ž(10) ๋งŒํผ ์ขŒ์ธก(+)์— 0์„ ์ฑ„์šฐ๊ณ  ์ (.) ์šฐ์ธก์— ํ•ด๋‹นํ•˜๋Š” ์ˆซ์ž๋งŒํผ ์†Œ์ˆ˜์  ์ดํ•˜์˜ ์ˆซ์ž๋ฅผ ์ž๋ฅธ๋‹ค.
%,.2f ์ฒœ๋‹จ์œ„ ์ฝค๋งˆ(,) ์ฒ˜๋ฆฌ๋ฅผ ํ•œ ํ›„ ์ (.) ์šฐ์ธก์— ํ•ด๋‹นํ•˜๋Š” ์ˆซ์ž(2)๋งŒํผ ์†Œ์ˆ˜์  ์ดํ•˜ ์ž๋ฆฌ๋ฅผ ๋ฐ˜์˜ฌ๋ฆผํ•˜์—ฌ ๊ธธ์ด๋ฅผ ๋งŒ๋“ ๋‹ค.

์›ํ•˜๋Š” ์œ„์น˜ ํŒŒ๋ผ๋ฉ”ํ„ฐ ์‚ฌ์šฉ

Syntax

% [์ˆซ์ž]$ [ํƒ€์ž…]

Example

String.format("%2$s %1$s", "1", "2");
System.out.printf("%1$tY.%1$tm.%1$td %1$tH:%1$tM:%1$tS.%1$tL", new Date().getTime());
2 1
2014.12.08 15:46:16.270

์ฐธ๊ณ ์ž๋ฃŒ

https://micropai.tistory.com/48

replace vs replaceAll

replace ๊ณผ replaceAll ์˜ ์ฐจ์ด์ ์€ replace ์ธ์ž๋ฅผ ๋ฌธ์ž์—ด ๊ฐ’์œผ๋กœ ๋ฐ›๊ณ  replaceAll ์€ ์ธ์ž๋ฅผ ์ •๊ทœ์‹์œผ๋กœ ๋ฐ›๋Š”๋‹ค.

์ •๊ทœ์‹

replaceAll ํ•จ์ˆ˜๋กœ ํŠน์ˆ˜๋ฌธ์ž๋ฅผ ์‚ฌ์šฉ์‹œ์— ๋ฌธ์ž๋กœ ์ธ์‹ํ•˜๋Š” ๋ฐฉ๋ฒ•

[] ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ๋“ค

  • * => [*]
  • + => [+]
  • $ => [$]
  • ! => [!]

๋“ฑ์ด ๋Œ€ํ‘œ์ ์ด๋ฉฐ ! # % & @ : ; - . < > , ~ ' ๋ฐฑ์ฟผํ„ฐ ('), ๋“ฑ์ด ์žˆ๋‹ค.

\ ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ๋“ค

  • ( => \\(
  • ) => \\)
  • { => \\{
  • } => \\}
  • ^ => \\^
  • [ => \\[
  • ] => \\]

ํŠน์ˆ˜๋ฌธ์ž ์‚ฌ์šฉ

% ์‚ฌ์šฉํ•˜๋ ค๋ฉด %% ๋‘๋ฒˆ ์ž…๋ ฅ

StringJoiner

๋ฌธ์ž์—ด์„ ์†์‰ฝ๊ฒŒ ๋ถ™์ผ์ˆ˜ ์žˆ๋‹ค.

String first = "๊ด€์šฐ";
String second = "์žฅ๋น„";
String third = "์กฐ์šด";
String fourth = "ํ™ฉ์ถฉ";
String fifth = "๋งˆ์ดˆ";

String + Operator

String ์˜คํ˜ธ๋Œ€์žฅ๊ตฐ = first + "-" + "second" + "-" + third + "-" + fourth + "-" + fifth;
System.out.println(์˜คํ˜ธ๋Œ€์žฅ๊ตฐ);

StringBuffer / StringBuilder

StringBuffer sb = new StringBuffer();
sb.append(first);
sb.append("-");
sb.append(second);
sb.append("-");
sb.append(third);
sb.append("-");
sb.append(fourth);
sb.append("-");
sb.append(fifth);
sb.append("-");

String ์˜คํ˜ธ๋Œ€์žฅ๊ตฐ = sb.toString();
System.out.println(์˜คํ˜ธ๋Œ€์žฅ๊ตฐ);

StringJoiner

StringJoiner strJoiner = new StringJoiner("-");

strJoiner.add(first);
strJoiner.add(second);
strJoiner.add(third);
strJoiner.add(fourth);
strJoiner.add(fifth);

String ์˜คํ˜ธ๋Œ€์žฅ๊ตฐ = strJoiner.toString();
System.out.println(์˜คํ˜ธ๋Œ€์žฅ๊ตฐ);
๊ด€์šฐ-์žฅ๋น„-์กฐ์šด-ํ™ฉ์ถฉ-๋งˆ์ดˆ

StringJoiner ๋กœ prefix ์™€ suffix ์‚ฌ์šฉํ•˜๊ธฐ

StringJoiner strJoiner = new StringJoiner("-", "[", "]");

strJoiner.add(first);
strJoiner.add(second);
strJoiner.add(third);
strJoiner.add(fourth);
strJoiner.add(fifth);

String ์˜คํ˜ธ๋Œ€์žฅ๊ตฐ = strJoiner.toString();
System.out.println(์˜คํ˜ธ๋Œ€์žฅ๊ตฐ);
[๊ด€์šฐ-์žฅ๋น„-์กฐ์šด-ํ™ฉ์ถฉ-๋งˆ์ดˆ]

stream ์‚ฌ์šฉํ•˜๊ธฐ

List<String> ์žฅ๊ตฐ๋“ค = Arrays.asList(first, second, third, fourth, fifth);

String ์˜คํ˜ธ๋Œ€์žฅ๊ตฐ = ์žฅ๊ตฐ๋“ค.stream().collect(Collectors.joining("-", "[", "]"));
System.out.println(์˜คํ˜ธ๋Œ€์žฅ๊ตฐ);

String.intern

String test = "test";

System.out.println(test == "test");
System.out.println(test == new String("test"));

์œ„ ์ฝ”๋“œ๋ฅผ Java SE 6 ์—์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜จ๋‹ค.

true
false

์œ„ ํ˜„์ƒ์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

System.out.println(test == new String("test").intern());

์ด๋Š” new ์—ฐ์‚ฐ์ž๋ฅผ ํ†ตํ•ด String ์„ ์ƒ์„ฑํ•˜๊ธฐ ๋˜๋ฉด Heap ์˜์—ญ์— ์กด์žฌํ•˜๊ฒŒ ๋˜๊ณ  ๋ฆฌํ„ฐ๋Ÿด์„ ์ด์šฉํ•  ๊ฒฝ์šฐ์—๋Š” String Constant Pool ์— ์กด์žฌํ•˜๊ฒŒ ๋œ๋‹ค.

๋ฆฌํ„ฐ๋Ÿด๋กœ ์„ ์–ธํ•  ๊ฒฝ์šฐ ๋‚ด๋ถ€์ ์œผ๋กœ String ์˜ intern() ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜๊ฒŒ ๋œ๋‹ค.
์ด๋Š” String Constant Pool ์— ์กด์žฌํ•˜๋Š”์ง€ ๊ฒ€์ƒ‰ํ•˜๋ฉฐ ๋งŒ์•ฝ ์žˆ๋‹ค๋ฉด ๊ทธ ์ฃผ์†Œ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์—†๋‹ค๋ฉด String Constrant Pool ์— ๋„ฃ๊ณ  ์ƒˆ๋กœ์šด ์ฃผ์†Œ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋œ๋‹ค.

Java SE 7 ์—์„œ๋Š” String Constrant Pool ์ด ๊ธฐ์กด์— Perm ์˜์—ญ์—์„œ Heap ์˜์—ญ์œผ๋กœ ์œ„์น˜๋ฅผ ๋ณ€๊ฒฝํ•˜์˜€๋‹ค.

์•„๋ž˜์™€ ๊ฐ™์ด ํ•ด์‹œ์ฝ”๋“œ๋ฅผ ํ™•์ธํ•ด๋ณด๋ฉด ์•Œ ์ˆ˜์žˆ๋‹ค.

String a = "Keyword";
String b = new String("Keyword");
String c = b.intern();
String d = "Keyword";

// Keyword
System.out.println(a);
// Keyword
System.out.println(b);
// Keyword
System.out.println(c);
// Keyword
System.out.println(d);

// false
System.out.println(a == b);
// false
System.out.println(b == c);
// true
System.out.println(a == c);
// true
System.out.println(a == d);

// true
System.out.println(a.equals(b));
// true
System.out.println(b.equals(c));
// true
System.out.println(a.equals(c));
// true
System.out.println(a.equals(d));

// 708049632
System.out.println(System.identityHashCode(a));
// 1887400018
System.out.println(System.identityHashCode(b));
// 708049632
System.out.println(System.identityHashCode(c));
// 708049632
System.out.println(System.identityHashCode(d));