Home Web Front-end JS Tutorial Summary of js methods to get the last digit of a string_javascript skills

Summary of js methods to get the last digit of a string_javascript skills

May 16, 2016 pm 04:31 PM
js string Obtain

Method 1: Use the charAt method under the String object

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

Copy code The code is as follows:

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

The

substr() method can extract the specified number of characters starting from the start subscript in the string.

Copy code The code is as follows:

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.

Copy code The code is as follows:

var str = “123456″;
spstr = str.split("");
spstr[spstr.length-1];

Method 4: Get it done with regular rules

Copy code The 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 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

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Where to get Google security code Where to get Google security code Mar 30, 2024 am 11:11 AM

Where to get Google security code

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

Recommended: Excellent JS open source face detection and recognition project

Detailed explanation of the method of converting int type to string in PHP Detailed explanation of the method of converting int type to string in PHP Mar 26, 2024 am 11:45 AM

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 determine whether a Golang string ends with a specified character Mar 12, 2024 pm 04:48 PM

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 check if a string starts with a specific character in Golang? Mar 12, 2024 pm 09:42 PM

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 repeat a string in python_python repeating string tutorial Apr 02, 2024 pm 03:58 PM

How to repeat a string in python_python repeating string tutorial

How to install dual SIM on Realme 12 Pro? How to install dual SIM on Realme 12 Pro? Mar 18, 2024 pm 02:10 PM

How to install dual SIM on Realme 12 Pro?

PHP string manipulation: a practical way to effectively remove spaces PHP string manipulation: a practical way to effectively remove spaces Mar 24, 2024 am 11:45 AM

PHP string manipulation: a practical way to effectively remove spaces

See all articles