Home > Mobile >  How to add <br> tag to sentence per every 120 bytes
How to add <br> tag to sentence per every 120 bytes

Time:10-17

For my convenient working environment, I tried to add new
tag in sentence which is over 120 bytes. I found the js code that counts the bytes of sentence. But I want to add
tag to the sentence per every 120bytes. What code i can use in js to that?

function fn_checkByte(obj){
    const maxByte = 100; //최대 100바이트
    const text_val = obj.value; //입력한 문자
    const text_len = text_val.length; //입력한 문자수
    
    let totalByte=0;
    for(let i=0; i<text_len; i  ){
        const each_char = text_val.charAt(i);
        const uni_char = escape(each_char); //유니코드 형식으로 변환
        if(uni_char.length>4){
            
            totalByte  = 2;
        }else{
            
            totalByte  = 1;
        }
    }
    
    if(totalByte>maxByte){
        alert('최대 100Byte까지만 입력가능합니다.');
            document.getElementById("nowByte").innerText = totalByte;
            document.getElementById("nowByte").style.color = "red";
        }else{
            document.getElementById("nowByte").innerText = totalByte;
            document.getElementById("nowByte").style.color = "green";
        }
    }
}



CodePudding user response:

JavaScript is somehow broken by design, when it comes to Unicode, because it did the same mistake as Java to use UTF-16. You have to use a dedicated library to get the splitting right. Take a look at the grapheme-splitter.

BTW: what you try to do is impossible, because it is possible, that the 120 bytes range is just in the middle of a character. You have to stop thinking in bytes, if you work with Unicode. Or you have to use 32 bits to store your characters.

CodePudding user response:

Here are the custom solution, you can use it in many cases:

function fn_checkByte(obj){
    const maxByte = 100; //최대 100바이트
    const text_val = obj.value; //입력한 문자
    const text_len = text_val.length; //입력한 문자수
    
    let totalByte=0;
    for(let i=0; i<text_len; i  ){
        const each_char = text_val.charAt(i);
        const uni_char = escape(each_char); //유니코드 형식으로 변환
        if(uni_char.length>4){
            
            totalByte  = 2;
        }else{
            
            totalByte  = 1;
        }
    }
    
    if(totalByte>maxByte){
        console.log("Total Byte: " totalByte);
        let length = recursion(parseInt(totalByte)); 
        let findDistance = totalByte-(checkStaticValue*count);
        totalByte = totalByte-findDistance;
        for(let i=length-1; i>=1; i--){
            if(i == 1){
                console.log(totalByte-checkStaticValue*i);
            }else{
                console.log(totalByte-checkStaticValue*i "<br>");
            }
        }
    }
}

let count = 0;
let checkStaticValue = 120;
function recursion(parameter){
    count  ;
    return (parameter >= checkStaticValue) ? recursion(parameter-checkStaticValue) : count ;
}

fn_checkByte({"value": 'Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32. Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. Arshad Iqbal the senior developer here (https://www.linkedin.com/in/arshadiqbalofficialprofile/)... The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.'})
  • Related