Deep_Dev
article thumbnail

๐Ÿ’ก 1. StringBuilder ์‚ฌ์šฉํ•˜๋Š” ์ด์œ 

๊ธฐ์กด String ํด๋ž˜์Šค์˜ ํ•œ๊ณ„

String ํด๋ž˜์Šค๋Š” ํ•œ๋ฒˆ ์ƒ์„ฑ๋˜๋ฉด ๋ฌธ์ž์—ด์ด ๋ณ€๊ฒฝ๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ ๋ถˆ๋ณ€ํ•œ ์„ฑ์งˆ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

๋”ฐ๋ผ์„œ ๊ธฐ์กด ๋ฌธ์ž์—ด์— ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์„ ์ถ”๊ฐ€ํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฒฝ์šฐ,

๋ฉ”๋ชจ๋ฆฌ์—์„œ ๋ฌธ์ž์—ด์ด ์ถ”๊ฐ€๋˜๋Š”๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ์ƒˆ๋กœ์šด ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ํ• ๋‹น๋˜์–ด ๋ฌธ์ž์—ด์ด ์ƒ์„ฑ๋œ๋‹ค.

๋”ฐ๋ผ์„œ ๋ฌธ์ž์—ด์˜ ๋ณ€๊ฒฝ์ด ๋นˆ๋ฒˆํ•œ ๊ฒฝ์šฐ StringBuilder ๋ฅผ ํ™œ์šฉํ•ด์•ผ ํ•œ๋‹ค.

 

์•„๋ž˜์™€ ๊ฐ™์ด String sb="tesrStr";๋กœ ์„ ์–ธํ•˜๊ณ  sb=sb.concat("12");๋ฅผ ์ˆ˜ํ–‰ํ•œ ๊ฒฝ์šฐ, sb ๋ฌธ์ž์—ด์€ ์ƒˆ ์ฃผ์†Œ๊ฐ€ ํ• ๋‹น๋œ๋‹ค.

๋ฌธ์ž์—ด์„ ๋ณ€๊ฒฝํ•  ๋•Œ๋งˆ๋‹ค ์ƒˆ๋กœ์šด ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ํ• ๋‹น๋˜๋Š” ๊ฒƒ์€ ์ž์›๋‚ญ๋น„๊ฐ€ ์ผ์–ด๋‚˜๊ธฐ ๋•Œ๋ฌธ์—

StringBuilder๋ฅผ ํ™œ์šฉํ•ด์•ผ ํ•œ๋‹ค.

 

 

๐Ÿ’ก 2. StringBuilder

StringBuilder ํด๋ž˜์Šค๋Š” ๋ฌธ์ž์—ด์„ ๋ณ€๊ฒฝํ•˜๊ฑฐ๋‚˜ ์ด์–ด๋ถ™์ด๋Š” ๊ฒฝ์šฐ ์ถ”๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ ์ƒ์„ฑ ์—†์ด ๊ธฐ์กด ๋ฌธ์ž์—ด์ด ํ™•์žฅ๋˜๋Š” ํด๋ž˜์Šค์ด๋‹ค.

String๊ณผ ๋‹ค๋ฅด๊ฒŒ ๋ฌธ์ž์—ด์ด ๋นˆ๋ฒˆํ•˜๊ฒŒ ๋ณ€๊ฒฝ๋ ๋•Œ ์‚ฌ์šฉํ•˜๋ฉด ์„ฑ๋Šฅ์ด ์ข‹๋‹ค.

StringBuilderํด๋ž˜์Šค๋Š” ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ ํ™˜๊ฒฝ์—์„œ ๋ฌธ์ž์—ด์˜ ์•ˆ์ „ํ•œ ๋ณ€๊ฒฝ์„ ๋ณด์žฅํ•ด์ฃผ์ง€ ์•Š๋Š” ํŠน์ง•์ด ์žˆ๋‹ค.

์ฆ‰, ์—ฌ๋Ÿฌ ์“ฐ๋ ˆ๋“œ๊ฐ€ ๋ฌธ์ž์—ด(๊ณต์œ ์ž์›)์— ๋™์‹œ์— ์ ‘๊ทผ/๋ณ€๊ฒฝ์ด ์ด๋ฃจ์–ด์ง€๋ฉด ์ˆ˜ํ–‰๊ฒฐ๊ณผ๊ฐ€ ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฒƒ์„ ๋งํ•œ๋‹ค.

 

๋ฌธ์ž์—ด ๋ณ€๊ฒฝ ์‹œ ๋ฉ”๋ชจ๋ฆฌ ์ƒํƒœ

stringBuilder ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์—ฌ๊ธฐ์— "test"๋ฌธ์ž์—ด ์ƒ์„ฑ

๊ทธ ํ›„, append() ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด "1"๊ณผ "2"๋ฌธ์ž์—ด์„ ์ถ”๊ฐ€

์ด ๋•Œ, append() ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด 1๊ณผ 2๊ฐ€ ์ƒˆ๋กœ ์ƒ์„ฑ๋˜๋Š”๊ฒƒ์ด ์•„๋‹Œ ํ•˜๋‚˜์˜ ๋ฉ”๋ชจ๋ฆฌ์— ๋ฌธ์ž์—ด์ด ๊ณ„์† ์ด์–ด๋ถ™์—ฌ์ ธ ๋ณ€๊ฒฝ๋œ๋‹ค.

์ฝ”๋“œ ์˜ˆ์‹œ

- StringBuilder sb ์„ ์–ธ

- ๋ฌธ์ž์—ด sb๊ฐ€ ํ• ๋‹น๋œ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ ์ถœ๋ ฅ

- ๋ฌธ์ž์—ด ์ถ”๊ฐ€

- ๋ฌธ์ž์—ด sb๊ฐ€ ํ• ๋‹น๋œ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ ์ถœ๋ ฅ

- StringBuilder ํด๋ž˜์Šค๋ฅผ String ํด๋ž˜์Šค๋กœ ๋ณ€ํ™˜

StringBuilder sb=new StringBuilder("test");
System.out.print(sb+""+System.identityHashCode(sb)); // ๋ฌธ์ž์—ด๊ณผ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ ์ถœ๋ ฅ
sb.append("2");
sb.append("3");
System.out.print(sb+""+System.identityHashCode(sb)); // ๋ฌธ์ž์—ด๊ณผ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ ์ถœ๋ ฅ
String temp=sb.toString();

์‹คํ–‰ ๊ฒฐ๊ณผ

๋ฌธ์ž์—ด "test"์— "1๊ณผ" "2"๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ๋„ ๋ฌธ์ž์—ด ์ฃผ์†Œ๊ฐ€ ๋ณ€๊ฒฝ๋˜์ง€ ์•Š๋Š”๋‹ค.

 

 

๐Ÿ’ก 3. StringBuilder ๋ฉ”์†Œ๋“œ 

[ String ํด๋ž˜์Šค์™€ ๋™์ผ ๋ฉ”์†Œ๋“œ ]

  • charAt()
  • indexOf() / lastIndexOf()
  • length()
  • replace()
  • substring()
  • toString()

append() : ๋ฌธ์ž์—ด ์ถ”๊ฐ€

StringBuilder sb=new StringBuilder("Hello");
sb.append(" world)";
System.out.print(sb); // "Hello World"

 

capacity()

  • Stringํด๋ž˜์Šค์™€ ๋‹ค๋ฅด๊ฒŒ char[]๋ฐฐ์—ด ์‚ฌ์ด์ฆˆ๋ฅผ ์—ฌ์œ  ์žˆ๊ฒŒ ์žก์•„๋‘ 
  • ํ˜„์žฌ char[] ๋ฐฐ์—ด์ด ๊ฐ€์ง„ ์‚ฌ์ด์ฆˆ ์ •๋ณด๋ฅผ ๋ฐ˜ํ™˜
  • length()๋Š” ์‹ค์ œ ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด์žˆ๋Š” ๋ฌธ์ž์—ด ์ž์ฒด์˜ ๊ธธ์ด๊ณ , capacity()๋Š” ํ˜„์žฌ ๋ฐฐ์—ด size
  • append() ๋“ฑ ๋ฌธ์ž์—ด ์กฐ์ •ํ•  ๋•Œ ๋ฐฐ์—ด ์‚ฌ์ด์ฆˆ๊ฐ€ ์ž๋™์œผ๋กœ ๋ณ€๊ฒฝ๋จ
StringBuilder sb = new StringBuilder("Hello");
		
		System.out.println(sb.length());		// 5
		System.out.println(sb.capacity()); 	// 21
		
		sb.append(" World");
		System.out.println(sb);             	// "Hello World"
		
		System.out.println(sb.length()); 	// 11
		System.out.println(sb.capacity()); 	// 21
		
		sb.append(" Hi everybody!!!!");
		System.out.println(sb);             	// Hello World Hi everybody!!!!

		System.out.println(sb.length()); 	// 28
		System.out.println(sb.capacity()); 	// 44

 

delete()

  • ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์€ ์ธ๋ฑ์Šค ์‚ฌ์ด์˜ ๋ฌธ์ž์—ด ์ œ๊ฑฐ
  • parameter : ์ธ๋ฑ์Šค ์‹œ์ž‘์ , ์ธ๋ฑ์Šค ๋์  + 1
  • ๋ฌธ์ž์—ด์—์„œ ์‹œ์ž‘๊ณผ ๋์€ ํ•ญ์ƒ "์‹œ์ž‘<=๋ฒ”์œ„<๋" ํ˜•ํƒœ
StringBuilder sb=new StrinbBuilder("Hello");
sb.append(" World");
System.out.print(sb); // "Hello World"

sb.delete(6,9);
System.out.print(sb); // "Hello ld"

 

deleteCharAt()

  • ํŠน์ • ์ธ๋ฑ์Šค์˜ ํ•œ ๋ฌธ์ž๋งŒ ์‚ญ์ œ
  • delete() ๋ฉ”์†Œ๋“œ์—์„œ ๋ฒ”์œ„๋ฅผ ํ•œ ๊ธ€์ž๋งŒ ์žก๋Š”๊ฒƒ๊ณผ ๋™์ผํ•œ ํšจ๊ณผ
StringBuilder sb=new StringBuilder("Hello");
sb.append(" World");
System.out.print(sb); // "Hello"

//sb.delete(6,7)๊ณผ ๊ฐ™์Œ
sb.deleteCharAt(6);
System.out.print(sb); // "Hello orld"

 

 

insert()

  • ํŠน์ • ์œ„์น˜์— ๋ฌธ์ž์—ด ์‚ฝ์ž…
  • ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์€ ์ธ๋ฑ์Šค ์œ„์น˜๋ถ€ํ„ฐ ๋ฌธ์ž์—ด์„ ์‚ฝ์ž…
StringBuilder sb=new StrinbBuilder("He World");
sb.inser(2, "llo");
System.out.print(sb); // "Hello World"

sb.insert(5,55);
System.out.print(sb); // "Hello55 World"

 

 

reverse()

  • ๋ฌธ์ž์—ด์„ ๊ฑฐ๊พธ๋กœ ๋’ค์ง‘์–ด์คŒ
StringBuilder sb=new StringBuilder("Hello World");

sb.reverse();
System.out.print(sb); // "dlroW olleH"

 

 

setCharAt()

  • ํŠน์ • ์œ„์น˜์˜ ๋ฌธ์ž ๋ณ€๊ฒฝ
  • insert()๊ฐ€ ์›๋ณธ ๋ฌธ์ž์—ด ์ค‘๊ฐ„์— ์‚ฝ์ž…์ด๋ผ๋ฉด setCharAt()์€ ํ•ด๋‹น ์œ„์น˜์˜ ๋ฌธ์ž๋ฅผ ๋ณ€๊ฒฝ
StringBuilder sb=new StringBuilder("Hello World");

sb.setCharAt(0,'h');
System.out.print(sb); // "hello World"

 

 

 

 

 

 

'๐Ÿ“š ์ฝ”๋”ฉํ…Œ์ŠคํŠธ > JAVA' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[JAVA] List : LinkedList  (0) 2022.10.26
[JAVA] List : ArrayList  (0) 2022.10.26
[JAVA] ๋ฌธ๋ฒ• : ํ˜•๋ณ€ํ™˜ ์ •๋ฆฌ  (0) 2022.10.26
[JAVA] String method  (0) 2022.10.26
[JAVA] LRU ( ์บ์‹œ )  (0) 2022.10.19