Example of using JS Charat

Source: Internet
Author: User

  This article mainly introduces the use of JS Charat, the need for friends can refer to the following

eg:    Code as follows: <html>  <body>    <script type= "Text/javascript" >    var str= "Hello world!"   document.write ("The character is:" + str.charat (0) + "<br/>")   document.write ("the second Charac TER is: "+ str.charat (1) +" <br/> ")   document.write (" The third character are: "+ Str.charat (2))     </script>    </body>  </html>    Results:    the character is:h&nb Sp The second character is:e  the third character is:l    definitions and usages     charAt () method returns the characters at the specified position.     Note that JavaScript does not have a character data type that is different from a string type, so the character returned is a string of length 1.     Syntax     Stringobject.charat (index)     parameter Description     index required. A number that represents a position in a string, that is, the subscript of a character in a string.     Hints and notes     NOTE: The subscript for the first character in a string is 0. If the argument index is not between 0 and String.Length, the method returns an empty string.     Instance     in the string "Hello world!", we will return the character of position 1:    code as follows: <script type= "Text/javascript" >    var str= "Hello world!"   document.write (Str.charat (1))     </script>    The output of the code is:    e    Returns the character at the specified index position.     Strobj.charat (index)     parameter     strobj    required option. Any String object or text.     index    required options. The zero-based index of the desired character. A valid value is a value between 0 and the length of the string minus 1.     Description     CharAt method returns a character value that is at the specified index position. The index of the first character in the string is 0, the second index is 1, and so on. An index value that is outside the valid range returns an empty string.     Examples     The following example illustrates the use of the CharAt method:  the   code as follows: function Charattest (n) {  var str = "Abcdefghij KLMNOPQRSTUVWXYZ "; Initializes the variable.   VAR S; Reputation variables.   s = Str.charat (n-1);  //Get the correct characters from the location where the index is n–1.   return (s); Returns the character.  }     

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.