Home Web Front-end JS Tutorial Detailed explanation of JavaScript substr() string interception function_Basic knowledge

Detailed explanation of JavaScript substr() string interception function_Basic knowledge

May 16, 2016 pm 05:36 PM
substr String interception

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

Syntax
stringObject.substr(start,length)

参数 描述
start 必需。要抽取的子串的起始下标。必须是数值。如果是负数,那么该参数声明从字符串的尾部开始算起的位置。也就是说,-1 指字符串中最后一个字符,-2 指倒数第二个字符,以此类推。
length 可选。子串中的字符数。必须是数值。如果省略了该参数,那么返回从 stringObject 的开始位置到结尾的字串。

Description
If length is 0 or negative, an empty string will be returned. If this parameter is not specified, the substring will be continued to the end of stringvar.

Example
The following example demonstrates the use of the substr method.

The first character to intercept the string.


[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]

The result is: j

[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
]


It means interception Starting from the 12th character in the string, the length is 5

In this example we will use substr() to extract some characters from a string:

[Ctrl A Select All Note: If you need to introduce external Js, you need to refresh to execute

]

Output:

lo world!

Example 2


[Ctrl A Select All Note:

If you need to introduce external Js, you need to refresh to execute

]

Output:

lo worl
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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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)

Methods to optimize Java string interception performance Methods to optimize Java string interception performance Jun 29, 2023 pm 10:51 PM

Title: How to optimize string interception performance in Java development In Java development, string interception is a common operation. However, due to the immutability of strings and the underlying implementation mechanism, string interception may cause performance issues. Therefore, we need to pay attention to optimizing string interception methods in daily development to improve program performance. Use appropriate parameters when using the String.substring() method. The substring() method in the String class is a commonly used string interception method.

Use Java's String.substring() function to intercept the substring of a string Use Java's String.substring() function to intercept the substring of a string Jul 25, 2023 pm 09:06 PM

Use java's String.substring() function to intercept a substring of a string. In the Java programming language, the String class provides a wealth of methods for manipulating strings. Among them, the String.substring() function is a commonly used method that can be used to intercept substrings of strings. This article will introduce how to use the String.substring() function to intercept strings, and provide some code examples for practical application scenarios. String.subst

PHP returns the ASCII value of the first character of the string PHP returns the ASCII value of the first character of the string Mar 21, 2024 am 11:01 AM

This article will explain in detail the ASCII value of the first character of the string returned by PHP. The editor thinks it is very practical, so I share it with you as a reference. I hope you can gain something after reading this article. PHP returns the ASCII value of the first character of a string Introduction In PHP, getting the ASCII value of the first character of a string is a common operation that involves basic knowledge of string processing and character encoding. ASCII values ​​are used to represent the numeric value of characters in computer systems and are critical for character comparison, data transmission and storage. The process of getting the ASCII value of the first character of a string involves the following steps: Get String: Determine the string for which you want to get the ASCII value. It can be a variable or a string constant

PHP returns the string from the start position to the end position of a string in another string PHP returns the string from the start position to the end position of a string in another string Mar 21, 2024 am 10:31 AM

This article will explain in detail how PHP returns the string from the start position to the end position of a string in another string. The editor thinks it is quite practical, so I share it with you as a reference. I hope you will finish reading this article. You can gain something from this article. Use the substr() function in PHP to extract substrings from a string. The substr() function can extract characters within a specified range from a string. The syntax is as follows: substr(string,start,length) where: string: the original string from which the substring is to be extracted. start: The index of the starting position of the substring (starting from 0). length (optional): The length of the substring. If not specified, then

How to use the substring() function of the String class in Java to implement string interception How to use the substring() function of the String class in Java to implement string interception Jul 25, 2023 am 08:19 AM

How does Java use the substring() function of the String class to implement string interception? In Java programming, the String class provides many methods for processing strings. Among them, the substring() function is a very commonly used function, which can implement the interception operation of strings. In this article, I will introduce how to use the substring() function to implement string interception and provide some code examples. First, let us understand the substring() function

Complete list of PHP string interception methods: mb_substr() is no longer applicable Complete list of PHP string interception methods: mb_substr() is no longer applicable Mar 15, 2024 pm 06:09 PM

In PHP, intercepting strings is one of the common operations, and one of the most commonly used functions is the mb_substr() function. However, as the PHP version is updated, the mb_substr() function may no longer be applicable in some cases, which may cause character encoding issues or performance issues. Therefore, this article will introduce some methods to implement string interception in PHP, as well as specific code examples to help developers better understand and apply these methods. 1. Use the mb_substr() function to intercept words

PHP regular expression: How to extract substrings between multiple specific characters from a string and concatenate them into a string PHP regular expression: How to extract substrings between multiple specific characters from a string and concatenate them into a string Jun 23, 2023 am 08:51 AM

PHP regular expressions are a powerful tool that can be used to match and extract specific text from strings. In this article, we will introduce how to use PHP regular expressions to extract substrings between multiple specific characters in a string and concatenate them into a string. Before we begin, let's take a look at some basic concepts in regular expressions: Pattern: the regular expression used for matching. For example, if we want to match all numbers in a string, then we can use the pattern: /d/charset: used to match a set of characters

Understand the substr() function in PHP to intercept strings Understand the substr() function in PHP to intercept strings Nov 18, 2023 am 11:27 AM

Understand the substr() function in PHP for intercepting strings. In the PHP language, the substr() function is a very useful string processing function, which can be used to intercept string fragments at a specified position and length. The substr() function accepts three parameters: the string to be intercepted, the starting position of the interception, and the length of the interception. Below we will introduce the use of the substr() function in detail and give specific code examples. Basic usage of substr() function substr() function

See all articles