java 여러줄 문자열 만들기

By | 2022년 6월 9일
Table of Content

java 여러줄 문자열 만들기

참조

String + String

String str = "<div>\n"
           + "    <p>Hello World</p>\n"
           + "</div>";

System.out.println(str);

StringBuilder

StringBuffer sb = new StringBuffer();
sb.append("<div>\n");
sb.append("    <p>Hello World</p>\n");
sb.append("</div>");

System.out.println(sb.toString());

String#format()

String str = String.format("%s\n%s\n%s"
        , "<div>"
        , "    <p>Hello World</p>"
        , "</div>");

System.out.println(str);

StringWriter

StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
pw.println("<div>");
pw.println("    <p>Hello World</p>");
pw.println("</div>");

System.out.println(sw.toString());

String#join()

String str = String.join("\n",
         "<div>",
         "    <p>Hello World</p>",
         "</div>");

System.out.println(str);

답글 남기기