


Summary of js methods to get the last digit of a string_javascript skills
May 16, 2016 pm 04:31 PMMethod 1: Use the charAt method under the String object
The charAt() method returns the character at the specified position.
str.charAt(str.length – 1)
Please note that JavaScript does not have a character data type distinct from the string type, so the characters returned are strings of length 1
Method 2: Use the substr method under the String object
Thesubstr() method can extract the specified number of characters starting from the start subscript in the string.
str.substr(str.length-1,1)
Important: ECMAscript does not standardize this method and therefore discourages its use.
Important: In IE 4, the value of parameter start is invalid. In this BUG, start specifies the position of the 0th character. (www.jb51.net) This BUG has been corrected in later versions.
Method 3: Use the split method under the String object
The split() method is used to split a string into an array of strings.
var str = “123456″;
spstr = str.split("");
spstr[spstr.length-1];
Method 4: Get it done with regular rules
<script type="text/javascript">
//<![CDATA[
var s = "nasofj;n234n41;v";
alert("String: " s "nn" "LastOne: " s.replace(/^(.*[n])*.*(.|n)$/g, "$2"));
//]]>
</script>
The above are the 4 methods that I know. They are all recorded. Friends in need can refer to them. If you have other methods, please let me know. Thank you

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Recommended: Excellent JS open source face detection and recognition project

Detailed explanation of the method of converting int type to string in PHP

How to determine whether a Golang string ends with a specified character

How to check if a string starts with a specific character in Golang?

How to repeat a string in python_python repeating string tutorial

How to install dual SIM on Realme 12 Pro?

PHP string manipulation: a practical way to effectively remove spaces
