본문 바로가기

.NET

HttpContext.Current.Response.Write 에 개행 \n 문자 넣는 방법 (chatGPT)

728x90
반응형

Replace 를 사용하여, "\n" 대신 "\\n" 를 넣는다.

using System;
using System.Web;

namespace YourNamespace
{
    public class YourClass
    {
        public void YourMethod()
        {
            // Get the current HttpContext
            HttpContext context = HttpContext.Current;

            // Set the content type of the response
            context.Response.ContentType = "text/html";

            // Construct the message with line breaks
            string sMsg = "안녕하세요.\n환영합니다";

            // Write the JavaScript alert with the formatted message
            context.Response.Write(string.Format("alert('{0}');", sMsg.Replace("\n", "\\n")));

            // End the response
            context.Response.End();
        }
    }
}

출처 | chatGPT

728x90
반응형