Js obtains the summary of the last string method, and js obtains the string summary.

Source: Internet
Author: User

Js obtains the summary of the last string method, and js obtains the string summary.

Method 1: Use the charAt method of the String object

The charAt () method returns characters at a specified position.

Copy codeThe Code is as follows:
Str. charAt (str. length-1)

Please note that JavaScript does not have a character data type different from the string type, so the returned character is a string with a length of 1

Method 2: Use the substr method under the String object

The substr () method extracts a specified number of characters starting with the start subscript from a string.

Copy codeThe Code is as follows:
Str. substr (str. length-1, 1)

Important: ECMAscript does not standardize this method, so it is opposed to using it.

Important: in IE 4, the value of start is invalid. In this BUG, start specifies the position of 0th characters. (Www.jb51.net) in a later version, this BUG has been fixed.

Method 3: Use the split method under the String object

The split () method is used to split a string into a string array.

Copy codeThe Code is as follows:
Var str = 123456 ″;
Spstr = str. split ("");
Spstr [spstr. length-1];

Method 4: RegEx

Copy codeThe Code is as follows:
<Script type = "text/javascript">
// <! [CDATA [
Var s = "nasofj; n234n41; v ";
Alert ("String:" + s + "nn" + "LastOne:" + s. replace (/^ (. * [n]) *. *(. | n) $/g, "$2 "));
//]>
</Script>

The above four methods have been recorded. If you need them, please refer to them. If you have other methods, please let me know. Thank you.

Related Article

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.