Home>Article>Web Front-end> How to get the last few digits of a string in javascript

How to get the last few digits of a string in javascript

醉折花枝作酒筹
醉折花枝作酒筹 Original
2021-06-16 10:29:01 20465browse

In JavaScript, you can use the substring method to remove the last few digits of a string. The syntax format is "string object.substring (first character position, last character position)"; this method is used to extract characters The character in the string between the two specified subscripts.

How to get the last few digits of a string in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

The code is as follows:

var str="abcdefghhhh";//截取后4位 str.substring(str.length-4);

The substring() method is used to extract the characters between two specified subscripts in the string.

Syntax

stringObject.substring(start,stop)

How to get the last few digits of a string in javascript

Return value

A new string value containing a substring of stringObject whose The content is all characters from start to stop-1, and its length is stop minus start.

Explanation

The substring returned by the substring() method includes the characters at start, but does not include the characters at stop.

If the parameters start and stop are equal, then this method returns an empty string (that is, a string with a length of 0). If start is greater than stop , then the method will swap the two parameters before extracting the substring.

[Recommended learning:javascript advanced tutorial]

The above is the detailed content of How to get the last few digits of a string in javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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