PROGRAMMING/JavaScript

[JavaScript] 한글 글자수 알아내기.

OJR 2010. 3. 10. 16:01
function get_strlen(str)
{
  var n=0;
  for( i=0;i<str.length;i++ )
  {
    var c = str.charAt(i);
    if(escape(c).length>4)
      n+=1;
    else if(c=='\n')
    {
      if(str.charAt(i-1) != '\r')
        n +=1;
    }
    else if(c =='<' || c=='>' )
      n+=4;
    else
      n+=1;
  }
  return n;
}

 

 

// Hangul character count function
function getHangulCharCount(str) {
  let count = 0;
  for (let i = 0; i < str.length; i++) {
    const charCode = str.charCodeAt(i);
    if (charCode >= 0xac00 && charCode <= 0xd7a3) {
      count++; // increment count for Hangul character
    }
  }
  return count;
}

// Example usage
const hangulText = "안녕하세요. Hello!";
const hangulCount = getHangulCharCount(hangulText);
console.log(`Number of Hangul characters: ${hangulCount}`); // output: 5

'PROGRAMMING > JavaScript' 카테고리의 다른 글

[js prototype] attribute 값 읽어오기  (0) 2010.04.05
[javascript] jQuery  (0) 2010.04.01
prototype 예 class 를 추가하거나 뺄수 있다.  (0) 2010.01.29
document.readyState  (0) 2010.01.25
JS - prototype element.update()  (0) 2010.01.19