In javascript, you can use the charAt () function to obtain characters at the specified position in a string. If you want to obtain the Unicode encoding of this character, what function should you use. The charCodeAt function in javascript can obtain the Unicode encoding of a specific character in a string. This article describes how to use charCodeAt. The charCodeAt () method returns the Unicode encoding of characters at the specified position. The returned value is an integer between 0 and 65535. This method is similar to the charAt () method, except that the former returns the encoding of the characters at the specified position, and the latter returns the character substrings.
Basic syntax
strObj.charCodeAt(index)
Parameter Introduction
Parameters |
Description |
Index |
Required. Number that counts the characters to be processed from scratch. The valid value is a number ranging from 0 to 1 minus the string length. |
Description
- The first character in a string is 0, the second character is 1, and so on.
- If the specified position does not contain any characters, NaN is returned.
Instance:
The following example illustrates the usage of the charCodeAt method.
Script function charCodeAtTest (n) {var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // initialize the variable. Var n; // declare the variable. N = str. charCodeAt (n-1); // obtain the Unicode value of the character at the position n. Return (n); // return this value .} Document. write (charCodeAtTest (3); // output the Unicode code of D, script
Online operation