Example of getting started with the JavaScript charCodeAt method (used to get the Unicode encoding of characters at the specified position), charcodeatunicode
JavaScript charCodeAt Method
The charCodeAt method is used to obtain the Unicode encoding of characters at the specified position from a string. The syntax is as follows:
Copy codeThe Code is as follows:
Str_object.charCodeAt (x)
Parameter description:
| Parameters |
Description |
| Str_object |
String (object) to be operated) |
| X |
Required. Number indicating the position |
Tip: the string is counted from 0.
CharCodeAt Method Instance
Copy codeThe Code is as follows:
<Script language = "JavaScript">
Document. write ("jb51". charCodeAt (1 ));
</Script>
Run this example and output:
Copy codeThe Code is as follows:
98
If the parameter x is not between 0 and the maximum length of the string, this method returns NaN.
The charCodeAt method is very 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.
The charCodeAt () method returns the Unicode encoding of characters at the specified position. The returned value is an integer between 0 and 65535. How can I use c #
How to convert unicode encoding to Chinese in javascript? Although unicode is viewed in a browser as Chinese,
After encoding conversion and reverse conversion, the following two functions can be applied directly.
Function stringtocode (name) {// character conversion to encoding separated by commas (,)
Var result = String ();
If (name = ''| name = undefined) return name;
For (var I = 0; I <name. length; I ++ ){
Result + = name. charCodeAt (I) + ",";
}
Result = result. substring (0, result. length-1 );
Return result;
}
Function codetostring (cod) {// convert the encoding to a character, separated by commas (,).
Var result = String (), a = new Array ();
A = cod. split (",");
For (var I = 0; I <a. length; I ++)
Result + = String. fromCharCode (parseInt (a [I]);
Return result;
}