Home Web Front-end JS Tutorial How to intercept a string in javascript

How to intercept a string in javascript

Apr 01, 2021 pm 06:00 PM
javascript string

How to intercept strings in javascript: 1. Use the substr() function to intercept the specified number of characters starting from the specified subscript in the string; 2. Use the substring() function to intercept the string intermediary The character between the two specified subscripts.

How to intercept a string in javascript

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

Method 1: Use substr()

<script type="text/javascript">
var str="Hello world!";
console.log(str);
console.log(str.substr(3,9));  //从倒数第六个字符开始,截取四位
</script>
Copy after login

Rendering:

How to intercept a string in javascript

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

Syntax:

substr(start,length)
Copy after login
  • start: The starting subscript of the substring to be intercepted must be a numerical value. If negative, this parameter is the position from the end of the string. That is to say, -1 refers to the last character in the string, -2 refers to the second to last character, and so on. You must write

  • length: the characters in the substring Number must be a numerical value. If this parameter is not filled in, the characters from the beginning to the end of the string are returned. If length is 0 or negative, an empty string will be returned.

Example:

Method 2: Use substring()

<script type="text/javascript">
var str="Hello world!";
console.log(str);
console.log(str.substring(3,9));  //从第三个字符开始到第八位
</script>
Copy after login

Rendering:

How to intercept a string in javascript

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

Syntax:

substring(start,stop)
Copy after login
  • start: a non-negative integer, which refers to the first character of the substring to be extracted. Position in the string, required element

  • #stop: a non-negative integer, 1 more than the position of the last character of the substring to be extracted on the string, optional You can write it or not. If you do not write it, the returned substring will go to the end of the string.

The length of the string is stop-start

If the parameters start and If stop is equal, the method returns an empty string. If start is greater than stop, the method will exchange the two parameters before extracting the substring.

For more programming related knowledge, please visit: Programming Video! !

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

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)

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 solve the problem of Chinese garbled characters when converting hexadecimal to string in PHP How to solve the problem of Chinese garbled characters when converting hexadecimal to string in PHP Mar 04, 2024 am 09:36 AM

How to solve the problem of Chinese garbled characters when converting hexadecimal to string in PHP

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

PHP String Matching Tips: Avoid Ambiguous Included Expressions PHP String Matching Tips: Avoid Ambiguous Included Expressions Feb 29, 2024 am 08:06 AM

PHP String Matching Tips: Avoid Ambiguous Included Expressions

PHP techniques for deleting the last two characters of a string PHP techniques for deleting the last two characters of a string Mar 23, 2024 pm 12:18 PM

PHP techniques for deleting the last two characters of a string

See all articles