Javascript charcodeat () and fromcharcode ()

Source: Internet
Author: User
1. charcodeat ()

Convert characters into code;

var str="Hello world!"document.write(str.charCodeAt(1))

  1. 'Cjk ': ['u4e00', 'u9fa5'], // Chinese Character [1-Example]
  2. 'Num': ['u0030', 'u0039 '], // number [0-9]
  3. 'Lal': ['u0061 ', 'u007a'], // lowercase letter [A-Z]
  4. 'Ual': ['u0041', 'u005a '], // uppercase letter [A-Z]
  5. 'Asc': ['u0020', 'u007e '] // ASCII visual character
2. fromcharcode ()

Convert code into characters

Definition and usage

Fromcharcode () accepts a specified Unicode value and returns a string.

Syntax
String.fromCharCode(numX,numX,...,numX)
Parameters Description
Numx Required. One or more Unicode values, that is, the Unicode encoding of the characters in the string to be created.
Tips and comments

Note: This method is a static string method. Each character in a string is specified by a separate numeric unicode encoding.

It cannot be used as a string object created by you. Therefore, its syntax should be string. fromcharcode (), rather than mystringobject. fromcharcode ().

Instance

In this example, "hello" and "ABC" are output based on UNICODE ":

<script type="text/javascript">document.write(String.fromCharCode(72,69,76,76,79))document.write("<br />")document.write(String.fromCharCode(65,66,67))</script>

Output of the above Code:

HELLOABC

3. Differences between charcodeat () and charat ()

Charcodeat (I) returns the Unicode encoding of characters at the specified position.

Charat (I) returns the character at the specified position.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.