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>");
댓글