Home  >  Article  >  Web Front-end  >  How to intercept the first four digits of a javascript string

How to intercept the first four digits of a javascript string

青灯夜游
青灯夜游Original
2021-03-09 15:45:2424024browse

How to intercept the first four digits of a javascript string: 1. Use "str.slice(0, 4)" to intercept the first four digits of the string; 2. Use "str.substr(0,4)" To intercept the first four digits of the string; 3. Use "str.substring(0,4)" to intercept the first four digits of the string.

How to intercept the first four digits of a javascript string

The operating environment of this tutorial: Windows 7 system, ECMAScript version 5, Dell G3 computer.

javascript intercepts the first four digits of the string

1. Use slice() to intercept

slice() method can extract a certain part of the string by specifying the start and end positions, and return the extracted part as a new string.

The syntax is as follows:

stringObject.slice(start, end)
  • start (required): Specifies where to start selecting. If negative, it specifies the position from the end of the string. That is, -1 refers to the last character, -2 refers to the second to last character, and so on.

  • end (optional): Specifies where to end the selection, that is, the character subscript at the end. If this parameter is not specified, the intercepted string contains all characters from start to end. If this parameter is negative, it specifies the characters starting from the end of the array.

Example:

var str = "0123456789";
console.log("原始字符串:", str);
console.log("截取字符串前四位:", str.slice(0, 4)); //0123

Rendering:

How to intercept the first four digits of a javascript string

2. Use substr( ) Intercept

substr method is used to return a substring of specified length starting from the specified position.

The syntax is as follows:

stringObject.substr(start, length)
  • start (required): The starting position of the desired substring. The first character in the string has index 0.

  • #length (optional): The number of characters that should be included in the returned substring.

Example:

var str = "hello world";
console.log("原始字符串:", str);
console.log("截取字符串前四位:", str.substr(0,4));  //hell

Rendering:

How to intercept the first four digits of a javascript string

Related recommendations:《JavaScript Video tutorial

3. Use substring() to intercept

The substring method is used to extract the characters between two specified subscripts in the string. The syntax is as follows:

stringObject.substring(start, stop)
  • start (required): A non-negative integer that specifies the position of the first character of the substring to be extracted in the stringObject.

  • stop (optional): A non-negative integer that is one more position in the stringObject than the last character of the substring to be extracted.

Return value: This method returns a new string. The string value contains a substring of stringObject, whose content is all characters from start to stop-1. , its length is stop minus start.

Example:

var str = "0123456789";
console.log("原始字符串:", str);
console.log("截取字符串前四位:", str.substring(0,4));  //0123

Rendering:

How to intercept the first four digits of a javascript string

##For more programming-related knowledge, please visit:

Programming Video ! !

The above is the detailed content of How to intercept the first four digits of a javascript string. 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