Method 1: Use the charAt method under the String object
The charAt() method returns characters at the specified position.
The code copy is as follows:
str.charAt(str.length 1)
Note that JavaScript does not have a character data type that is different from the string type, so the returned character is a string of length 1
Method 2: Use the substr method under the String object
The substr() method extracts a specified number of characters starting from the start subscript in a string.
The code copy is as follows:
str.substr(str.length-1,1)
Important: ECMAscript does not standardize the method, so it is opposed to using it.
Important: In IE 4, the value of parameter start is invalid. In this bug, start specifies the position of the 0th character. (www.VeVB.COM) In subsequent versions, 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.
The code copy is as follows:
var str = “123456”;
spstr = str.split("");
spstr[spstr.length-1];
Method 4: Regularly
The code copy 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 are the 4 methods I know. All of them have been recorded. Friends in need can refer to them. If there are any other methods, please tell me. Thank you