Miêu tả

Phương thức này trả về một số chỉ dẫn giá trị Unicode của ký tự tại chỉ mục đã cho.

Mã Unicode chỉ dãy từ 0 tới 1,114,111. Các điểm 128 mã Unicode đầu tiên là một so khớp trực tiếp của mã hóa ký tự ASCII. charCodeAt() luôn luôn trả về một giá trị mà nhỏ hơn 65,536.

Cú pháp

Sử dụng cú pháp sau để tìm mã hóa ký tự tại một chỉ mục cụ thể.

string.charCodeAt(index);

Chi tiết về tham số

index − Một integer giữa 0 và một số nhỏ hơn độ dài chuỗi; nếu không được xác định thì mặc định là 0.

Trả về giá trị

Trả về một số

Bạn thử ví dụ sau:

<html>
   <head>
      <title>JavaScript String charCodeAt() Method</title>
   </head>
   
   <body>
   
      <script type="text/javascript">
         var str = new String( "This is string" );
         document.write("str.charCodeAt(0) is:" + str.charCodeAt(0)); 
         document.write("<br />str.charCodeAt(1) is:" + str.charCodeAt(1)); 
         document.write("<br />str.charCodeAt(2) is:" + str.charCodeAt(2)); 
         document.write("<br />str.charCodeAt(3) is:" + str.charCodeAt(3)); 
         document.write("<br />str.charCodeAt(4) is:" + str.charCodeAt(4)); 
         document.write("<br />str.charCodeAt(5) is:" + str.charCodeAt(5)); 
      </script>
      
   </body>
</html>

Kết quả

str.charCodeAt(0) is:84
str.charCodeAt(1) is:104
str.charCodeAt(2) is:105
str.charCodeAt(3) is:115
str.charCodeAt(4) is:32
str.charCodeAt(5) is:105 

Các bài học JavaScript khác tại s2sontech:




Bình luận (0)

Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Michael Gough
Learning English Everyday