JS get string Last method Rollup _javascript tips

Source: Internet
Author: User

Method One: Using the Charat method under String Object

The CharAt () method returns the character at the specified position.

Copy Code code as follows:

Str.charat (str.length–1)

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

Method Two: Using the Substr method under the String object

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

Copy Code code as follows:

Str.substr (str.length-1,1)

Important: ECMAscript does not standardize the method and is therefore opposed to its use.

Important: In IE 4, the value of the parameter start is invalid. In this BUG, start sets the position of the No. 0 character. (www.jb51.net) in a later version, this BUG has been fixed.

Method Three: Using the Split method under String object

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

Copy Code code as follows:

var str = "123456″;
Spstr = Str.split ("");
SPSTR[SPSTR.LENGTH-1];

Method Four: Just fix it

Copy Code code as follows:

<script type= "Text/javascript" >
<! [cdata[
var s = "Nasofj;n234n41;v";
Alert ("String:" +s+ "nn" + "LaStone:" +s.replace (/^ (. *[n)) *.* (. | N) $/g, "$");
]]>
</script>

The above is I understand 4 kinds of methods, have recorded down, have the need of friends can refer to, if there are other methods also please tell, thank

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.