[JAVA] StringBuffer?๐Ÿคท๐Ÿปโ€โ™‚๏ธ

Lee ๐Ÿง™๐Ÿปโ€โ™‚๏ธยท2021๋…„ 8์›” 30์ผ
0
post-thumbnail

๐Ÿ‘จ๐Ÿปโ€๐Ÿซ StringBuffer

์‰ฝ๊ฒŒ ๋งํ•ด StringBuffer๋Š” ๋ฌธ์ž์—ด์„ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ๋ณ€๊ฒฝ ํ•  ๋•Œ ์ฃผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ์ž๋ฃŒํ˜•์ž…๋‹ˆ๋‹ค.

๐Ÿคท๐Ÿปโ€โ™‚๏ธ StringBuffer๋ฅผ ์™œ ์“ธ๊นŒ?

Stirng ๋ฌธ์ž์—ด์„ "+", concat()์œผ๋กœ ๊ฒฐํ•ฉํ•˜๋ฉด, ๋‚ด์šฉ์ด ํ•ฉ์ณ์ง„ ์ƒˆ๋กœ์šด String ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ String์€ ๋ถˆ๋ณ€(Immutable) ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ฌธ์ž์—ด์„ ๋งŽ์ด ๊ฒฐํ•ฉํ•˜๋ฉด ๊ฒฐํ•ฉํ• ์ˆ˜๋ก ๊ณต๊ฐ„์˜ ๋‚ญ๋น„ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์†๋„ ๋˜ํ•œ ๋งค์šฐ ๋Š๋ ค์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
์œ„์— ์‚ฌ์ง„์„ ๋ณด๋ฉด String.concat์„ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋ฉด ํ•  ์ˆ˜๋ก ์†๋„๊ฐ€ ๋Š๋ ค์ง€๋Š” ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
ํ•˜์ง€๋งŒ StringBuffer๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฌธ์ž์—ด์„ ๋ฐ”๋กœ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ๊ณต๊ฐ„์˜ ๋‚ญ๋น„๋„ ์—†์œผ๋ฉฐ ์†๋„๋„ String์— ๋น„ํ•ด ๋งค์šฐ ๋นจ๋ผ์ง‘๋‹ˆ๋‹ค.

๊ทธ ์ด์œ ๋Š” StringBuffer๋Š” Mutableํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค..

  • Mutable class(๊ฐ€๋ณ€ ํด๋ž˜์Šค) : ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜๋„ ์žˆ๊ณ , ์ถ”๊ฐ€ํ•  ์ˆ˜๋„ ์žˆ๋Š” ํด๋ž˜์Šค๋ฅผ ๋ง ํ•ฉ๋‹ˆ๋‹ค.
  • Immutable class(๋ถˆ๋ณ€ ํด๋ž˜์Šค) : ๊ฐ’์„ ์ฝ๊ธฐ๋งŒ ํ•  ์ˆ˜ ์žˆ๊ณ , ๋ณ€๊ฒฝํ•  ์ˆ˜๋„ ์—†๋Š” ํด๋ž˜์Šค๋ฅผ ๋ง ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿคท๐Ÿปโ€โ™‚๏ธ ๊ทธ๋ ‡๋‹ค๋ฉด ๋ฌด์กฐ๊ฑด StringBuffer๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์„๊นŒ?

๊ทธ๊ฑด ์ƒํ™ฉ์— ๋”ฐ๋ผ ๋‹ค๋ฅด๊ฒŒ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค! StringBuffer ์ž๋ฃŒํ˜•์€ String์— ๋น„ํ•ด ๋ฌด๊ฑฐ์šด ํŽธ์— ์†ํ•ฉ๋‹ˆ๋‹ค. new StringBuffer();๋กœ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ด String์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰๋„ ๋งŽ๊ณ  ์†๋„๋„ ๋Š๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์ž์—ด ์ถ”๊ฐ€๋‚˜ ๋ณ€๊ฒฝ๋“ฑ์˜ ์ž‘์—…์ด ๋งŽ์œผ๋ฉด StringBuffer๋ฅผ, ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด String์„ ์‚ฌ์šฉํ•˜๋Š”๊ฒŒ ์œ ๋ฆฌํ•ฉ๋‹ˆ๋‹ค!

๐Ÿ‘จ๐Ÿปโ€๐Ÿ’ป ์ฃผ์š” ๋ฉ”์†Œ๋“œ!

append

public class Test {
	public static void main(String[] args) {
    		StringBuffer sb = new StringBuffer();
        		sb.append("hello");
        		sb.append(" ");
        		sb.append("welcome to java");
        		System.out.println(sb.toString());
         }
}

๊ฒฐ๊ณผ๊ฐ’์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚˜์˜ต๋‹ˆ๋‹ค.
hello welcome to java
StringBuffer์˜ append๋ผ๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ณ„์†ํ•ด์„œ ๋ฌธ์ž์—ด์„ ์ถ”๊ฐ€ํ•ด ๋‚˜๊ฐˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์œ„ ์˜ˆ์ œ์™€ ๊ฐ™์ด toString()์„ ์‚ฌ์šฉํ•˜๋ฉด String ์ž๋ฃŒํ˜•์œผ๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

insert

insert ๋ฉ”์†Œ๋“œ๋Š” ํŠน์ • ์œ„์น˜์— ์›ํ•˜๋Š” ๋ฌธ์ž์—ด์„ ์‚ฝ์ž…ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

public class Test {
	public static void main(String[] args) {
    		StringBuffer sb = new StringBuffer();
        		sb.append("welcome to java");
            		sb.insert(0, "hello");
        		System.out.println(sb.toString());
         }
}

๊ฒฐ๊ณผ๊ฐ’์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚˜์˜ต๋‹ˆ๋‹ค.
hello welcome to java
sb.insert(์ธ๋ฑ์Šค, ๊ฐ’) 0 ๋ฒˆ์งธ ์œ„์น˜์— "hello"๋ผ๋Š” ๋ฌธ์ž์—ด์„ ์‚ฝ์ž…!

substring

substring ๋ฉ”์†Œ๋“œ๋Š” String์˜ substring ๋ฉ”์†Œ๋“œ์™€ ์‚ฌ์šฉ๋ฒ•์ด ๋™์ผํ•ฉ๋‹ˆ๋‹ค.

public class Test {
	public static void main(String[] args) {
    		StringBuffer sb = new StringBuffer();
        		sb.append("Hello welcome to java");
        		System.out.println(sb.substring(0, 4));
         }
}

๊ฒฐ๊ณผ๊ฐ’์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚˜์˜ต๋‹ˆ๋‹ค.
Hell
substring(์‹œ์ž‘์œ„์น˜, ๋์œ„์น˜)์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋ฉด StringBuffer ๊ฐ์ฒด์˜ ์‹œ์ž‘์œ„์น˜์—์„œ ๋์œ„์น˜๊นŒ์ง€์˜ ๋ฌธ์ž๋ฅผ ๋ฝ‘์•„๋‚ด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

๊ทธ์™ธ ๋Œ€ํ‘œ์ ์ธ ๋ฉ”์†Œ๋“œ๋“ค

Method์„ค๋ช…
int capacity()ํ˜„์žฌ ๋ฒ„ํผ ํฌ๊ธฐ๋ฅผ ๋ฐ˜ํ™˜ํ•จ.
StringBuffer delete(int start, int end)์ „๋‹ฌ๋œ ์ธ๋ฑ์Šค์— ํ•ด๋‹นํ•˜๋Š” ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ํ•ด๋‹น ๋ฌธ์ž์—ด์—์„œ ์ œ๊ฑฐํ•จ.
StringBuffer deleteCharAt(int index)์ „๋‹ฌ๋œ ์ธ๋ฑ์Šค์— ํ•ด๋‹นํ•˜๋Š” ๋ฌธ์ž๋ฅผ ํ•ด๋‹น ๋ฌธ์ž์—ด์—์„œ ์ œ๊ฑฐํ•จ.
StringBuffer reverse()ํ•ด๋‹น ๋ฌธ์ž์—ด์˜ ์ธ๋ฑ์Šค๋ฅผ ์—ญ์ˆœ์œผ๋กœ ์žฌ๋ฐฐ์—ดํ•จ.
profile
๋” ๋‚˜์€ ๊ฐœ๋ฐœ์ž๊ฐ€ ๋˜๊ธฐ ์œ„ํ•ด ๊ธฐ๋กํ•ฉ์„ธ!๐Ÿง™๐Ÿปโ€โ™‚๏ธ

0๊ฐœ์˜ ๋Œ“๊ธ€