Home > Web Front-end > JS Tutorial > body text

How to remove the first two digits of a string in javascript

青灯夜游
Release: 2022-02-23 14:48:40
Original
10288 people have browsed it

Javascript method to remove the first two digits of a string: 1. Use the slice() function, the syntax "string.slice(2)"; 2. Use the substring() function, the syntax "string.substring(2)" ".

How to remove the first two digits of a string in javascript

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

javascript removes the first two digits of the string

Method 1: Use the slice() function

## The #slice(start, end) method extracts a portion of a string and returns the extracted portion as a new string.

Use the start (inclusive) and end (exclusive) parameters to specify the part of the string to extract.

  • start The first character position in the parameter string is 0, the second character position is 1, and so on. If it is a negative number, it means how many strings to intercept from the tail, slice (-2) means extracting the second to last element in the original array to the last element (including the last element).

  • #If the end parameter is a negative number, -1 refers to the position of the last character of the string, -2 refers to the second to last character, and so on.

Example:

var str="ab12345";
console.log(str.slice(2))   //   =>  12345
Copy after login

How to remove the first two digits of a string in javascript

Method 2: Use substring() function

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

The substring() method returns a substring that includes the characters at the beginning, but does not include the characters at the end.

Example:

var str="ab12345";
console.log(str.substring(2))   //   =>  12345
Copy after login

How to remove the first two digits of a string in javascript

[Related recommendations:

javascript learning tutorial]

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

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!