본문 바로가기
Today I learned

2020 10 29

by soheemon 2020. 10. 29.

HTML element를 JAVA의 StringBuffer로 선언해서 사용할 수 있도록 도와주는 Util..

<textarea style="margin: 0px; height: 636px; width: 417px;" id="before">
</textarea>
<textarea style="margin: 0px; height: 636px; width: 417px;" id="after">
</textarea>
<div style="display:block">
    <button id="btn" onclick="makeStrBufCode()">
    변환하기
    </button>
    <button id="btn" onclick="copyToClipBoard()">
    클립보드에 복사
    </button>
    <button id="btn" onclick="cleanTextArea()">
    지우기
    </button>
</div>

<script>

function makeStrBufCode(){
    var convert = (document.getElementById('before').value || "").replaceAll('"', '\\"');    //"문자를 \"로 대체
    if(convert == "") return;
    var elements = convert.split("\n");
    var res = [];    //결과물 저장.
    var sbName = "sb";
    res.push("StringBuffer " +  sbName + " = new StringBuffer();" + '\n');
    res.push(sbName + ".setLength(0);" + '\n');

    let tmp;

    for(let i = 0; i < elements.length; i++){
        tmp = sbName + '.append(' + '"' + elements[i] + '"' + ')' + ';';
        res.push(tmp);
    }
    document.getElementById('after').value = res.join('\n');
}

function copyToClipBoard(){
    var afterElement = document.getElementById('after');
    afterElement.select();
    document.execCommand("copy");
}
function cleanTextArea(){
    document.getElementById('before').value = "";
    document.getElementById('after').value = "";
}
</script>

before

<div>
    <p id="so_difficult">퍼블리싱은 너무 어려워요.</p>
    <a href="www.tistory.com"> 티스토리로 이동하기</a>
</div>

after

        StringBuffer sb = new StringBuffer();

        sb.setLength(0);

        sb.append("<div>");
        sb.append("    <p id=\"so_difficult\">퍼블리싱은 너무 어려워요.</p>");
        sb.append("    <a href=\"www.tistory.com\"> 티스토리로 이동하기</a>");
        sb.append("</div>");

'Today I learned' 카테고리의 다른 글

2020 11 03  (0) 2020.11.03
2020 11 01  (0) 2020.11.01
2020 10 26  (0) 2020.10.26
2020 10 25  (0) 2020.10.25
2020 10 24  (0) 2020.10.24

댓글