๐Ÿ’ป ๊ณต๋ถ€ ๊ธฐ๋ก/โ˜• Java

Java | StringBuilder

  • -

์šฐ๋ฆฌ๋Š” ํ”ํžˆ String ๊ฐ์ฒด๋ผ๋ฆฌ ๋”ํ•ด์„œ ์‚ฌ์šฉํ•  ๋•Œ ์ด๋ ‡๊ฒŒ ์‚ฌ์šฉํ•œ๋‹ค.

String result1 = "์„ ์ƒ๋‹˜ ";
String result2 = "์•„์ด๋“ค";

System.out.println(result1 += result2); // ์„ ์ƒ๋‹˜ ์•„์ด๋“ค

 

๊ทธ๋Ÿฐ๋ฐ ์œ„์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์€ String ๊ฐ์ฒด๋ผ๋ฆฌ ๋”ํ•˜๋ฉด์„œ ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น๊ณผ ํ•ด์ฒด๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๋ฉฐ, ์—ฐ์‚ฐ์ด ๋งŽ์•„์งˆ ์ˆ˜๋ก ์„ฑ๋Šฅ์ด ๋–จ์–ด์ง„๋‹ค๊ณ  ํ•œ๋‹ค.

์ฐธ๊ณ ๊ธ€

" ๋งŽ์€ ๋ฌธ์ž์—ด์„ ์—ฐ๊ฒฐํ•˜๋ฉด ๋งŽ์€ ์ค‘๊ฐ„ ๋ฌธ์ž์—ด ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜์–ด ๋น„ํšจ์œจ์ ์ธ ์ฝ”๋“œ๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค "

 

String

์ž๋ฐ”์—์„œ๋Š” String ๊ฐ์ฒด๋Š” ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€๋Šฅ. ํ•œ ๋ฒˆ ์ƒ์„ฑ๋˜๋ฉด ๋‚ด์šฉ์„ ๋ฐ”๊ฟ€ ์ˆ˜ ์—†์Œ.
ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด์„ ๋‹ค๋ฅธ ๋ฌธ์ž์—ด๊ณผ ์—ฐ๊ฒฐํ•˜๋ฉด ์ƒˆ ๋ฌธ์ž์—ด์ด ์ƒ์„ฑ๋˜๊ณ , ์ด์ „ ๋ฌธ์ž์—ด์€ ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰ํ„ฐ๋กœ ๋“ค์–ด๊ฐ„๋‹ค.

 

StringBuilder

StringBuilder ๋Š” ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•œ ๋ฌธ์ž์—ด์„ ๋งŒ๋“ค์–ด์ฃผ๊ณ , ์ˆ˜ ๋ฐฑ๋ฒˆ์˜ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ๋•Œ ๋ฉ”๋ชจ๋ฆฌ ๋ถ€๋‹ด์„ ๋œ์–ด์ค€๋‹ค.

์‚ฌ์šฉ๋ฒ•

public class TestStringBuilder {
	public static void main(String[] args) {
		
        // StringBuilder
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("๋ฌธ์ž์—ด ").append("์—ฐ๊ฒฐ");
        
        // String์— StringBuilder๋ฅผ ๊ทธ๋Œ€๋กœ ๋„ฃ์„ ์ˆœ ์—†๋‹ค. toString()์„ ๋ถ™์—ฌ์•ผ ํ•œ๋‹ค
        String str = stringBuilder.toString();
        
        // ๋‘ println()์€ ๊ฐ™์€ ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค
        System.out.println(stringBuilder); // ๋ฌธ์ž์—ด ์—ฐ๊ฒฐ
        System.out.println(str); // ๋ฌธ์ž์—ด ์—ฐ๊ฒฐ
    }

}

 

์‚ฌ์šฉ๋ฒ• | ๋ฐ˜๋ณต๋ฌธ

public class TestStringBuilder {
	public static void main(String[] args) {
		StringBuilder stringBuilder = new StringBuilder();
		ArrayList<String> list = new ArrayList<>();
		list.add("์ฒซ ๋ฒˆ์งธ, ");
		list.add("๋‘ ๋ฒˆ์งธ, ");
		list.add("์„ธ ๋ฒˆ์งธ, ");
		list.add("๋„ค ๋ฒˆ์งธ, ");
		list.add("๋‹ค์„ฏ ๋ฒˆ์งธ");
		for (int i = 0; i < list.size(); i++) {
			stringBuilder.append(list.get(i));
		}
		System.out.println(stringBuilder); // ์ฒซ ๋ฒˆ์งธ, ๋‘ ๋ฒˆ์งธ, ์„ธ ๋ฒˆ์งธ, ๋„ค ๋ฒˆ์งธ, ๋‹ค์„ฏ ๋ฒˆ์งธ
	}
}

 

 

 

์ถœ์ฒ˜ | ์ฐธ๊นจ๋นต์œ„์—์ฐธ๊นจ๋นต

 

'๐Ÿ’ป ๊ณต๋ถ€ ๊ธฐ๋ก > โ˜• Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Java | Session  (1) 2023.01.18
Java | Cookie  (0) 2023.01.17
Java | ๋ฌธ์ž์—ด JSON ๋ณ€ํ™˜  (0) 2023.01.17
Java/JSP | HttpServlet  (0) 2023.01.17
Java | ObjectMapper  (0) 2023.01.17
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.