Home Web Front-end JS Tutorial Detailed explanation of commonly used interception string substr(), substring(), slice() methods_javascript skills

Detailed explanation of commonly used interception string substr(), substring(), slice() methods_javascript skills

May 16, 2016 pm 03:25 PM
slice substr substring

slice()

Definition: Accepts one or two parameters, the first parameter specifies the starting position of the substring. The second parameter represents the end position of the substring (excluding the character at the end position). If the second parameter is not passed, the length of the string is used as the end position.

1. When the passed parameter is a positive value:

var str ="helloWorld";

// 一个参数,则将字符串长度作为结束位置
alert(str.slice(3)); // "loWorld"

// 两个参数,7位置上的字符为"r",但不包括结束位置的字符
alert(str.slice(3,7)); // "loWo"

Copy after login

2. When the passed parameter is a negative value:

The

slice() method adds the passed negative value to the length of the string.

var str ="helloWorld";

// 一个参数,与字符串长度相加即为slice(7)
alert(str.slice(-3)); // "rld"

// 两个参数,与字符串长度相加即为slice(3,6)
alert(str.slice(3,-4)); // "loW"

Copy after login

3. When the second parameter is smaller than the first parameter:

If the second parameter passed in by the slice() method is smaller than the first parameter, an empty string will be returned.

var str ="helloWorld";
alert(str.slice(5,3)); // ""
Copy after login

4. IE compatibility

When tested with the IE8 browser, there is no problem and the behavior is consistent with modern browsers.

substring()

Definition: Accepts one or two parameters, the first parameter specifies the starting position of the substring. The second parameter represents the end position of the substring (excluding the character at the end position). If the second parameter is not passed, the length of the string is used as the end position.

1. When the passed parameter is a positive value: the same behavior as the slice() method

var str ="helloWorld";

// 一个参数,则将字符串长度作为结束位置
alert(str.substring(3)); // "loWorld"

// 两个参数,7位置上的字符为"r",但不包括结束位置的字符
alert(str.substring(3,7)); // "loWo"

Copy after login

2. When the passed parameter is a negative value:

substring()方法会把所有负值参数转换为0。来看下例子:

var str ="helloWorld";

// 两个参数,-4会转换为0,相当于substring(3,0) -->即为 substring(0,3)
alert(str.substring(3,-4)); // "hel"

Copy after login
The

substring() method will use the smaller number as the starting position and the larger number as the ending position. As in the above example, substring(3,0) and substring(0,3) have the same effect.

4. IE compatibility

When tested with the IE8 browser, there is no problem and the behavior is consistent with modern browsers.

substr()

Definition: Accepts one or two parameters, the first parameter specifies the starting position of the substring. The second parameter is somewhat different from the previous method, indicating the number of characters returned. If no second argument is passed, the length of the string is used as the ending position. Let’s see an example:

1. When the passed parameter is a positive value:

var str ="helloWorld";

// 一个参数,则将字符串长度作为结束位置
alert(str.substr(3)); // "loWorld"

// 两个参数,从位置3开始截取后面7个字符
alert(str.substr(3,7)); // "loWorld"

Copy after login

2. When the passed parameter is a negative value:

The

substr() method adds the negative first parameter to the length of the string and converts the negative second parameter to 0.

var str ="helloWorld";

// 将第一个负的参数加上字符串的长度--->
//即为:substr(7,5) ,从位置7开始向后截取5个字符
alert(str.substr(-3,5)); // "rld"

// 将第二个参数转换为0
// 即为:substr(3,0),即从位置3截取0个字符串,则返回空
alert(str.substr(3,-2)); // ""

Copy after login

3. IE compatibility

The substr() method will have problems when passing negative values, and the original string will be returned. IE9 fixes this issue.

Summary

The behavior of slice() and substring () is consistent when passing positive parameters, and the substr() method will be easily confused on the second parameter

When passing negative parameters, the slice() method adds the length of the string, which is in line with general thinking. Converting the second parameter of substring() to 0 will easily cause problems, and the starting position will be easy to change.

IE compatibility issues may occur when the substr() method has a negative value.

To sum up, I generally recommend using the slice() method.

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

How to solve the 'panic: runtime error: slice bounds out of range' error in golang? How to solve the 'panic: runtime error: slice bounds out of range' error in golang? Jun 25, 2023 pm 12:42 PM

In the Go language, we often encounter an error, namely "panic:runtimeerror:sliceboundsoutofrange" (slice out of range) error. This is because when we use slices, we usually access or operate the slice, and the access index may go out of bounds. This article will explain the basic causes of this error, how to avoid and solve it. 1. Cause of slice out-of-bounds error Slice is a reference type, which is controlled by the underlying

How to use the substring() function of the StringBuilder class in Java to intercept the substring of a string How to use the substring() function of the StringBuilder class in Java to intercept the substring of a string Jul 24, 2023 pm 12:13 PM

How does Java use the substring() function of the StringBuilder class to intercept a substring of a string? In Java, we often need to process string operations. Java's StringBuilder class provides a series of methods to facilitate us to operate strings. Among them, the substring() function can be used to intercept substrings of strings. The substring() function has two overloaded forms, namely substring(intstar

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 get a substring using String.substring() method in Java? How to get a substring using String.substring() method in Java? Nov 18, 2023 am 08:07 AM

How to get a substring using String.substring() method in Java? The String class in Java provides a very useful method substring(), which can be used to obtain the substring of a string. It allows us to select a portion of characters from a string and return it as a new string. This article will explain how to use the substring() method in Java and provide some code examples. Using the substring() method is very

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

Use the PHP function 'substr' to get the substring of a string Use the PHP function 'substr' to get the substring of a string Jul 24, 2023 pm 10:13 PM

Use the PHP function "substr" to obtain the substring of a string. In PHP programming, you often encounter situations where you need to obtain part of the content of a string. At this time, we can use PHP's built-in function "substr" to achieve this. This article explains how to use the "substr" function to get a substring of a string and provides some code examples. 1. Basic usage of the substr function The substr function is used to obtain a substring of a specified length from a string. Its basic syntax is as follows: substr(

PHP mb_substr function invalid solution PHP mb_substr function invalid solution Mar 22, 2024 am 09:00 AM

Solutions for invalid PHPmb_substr function When developing PHP applications, the mb_substr function is often used to intercept strings. However, sometimes you may encounter situations where the mb_substr function is invalid, mainly due to character encoding issues in different environments. In order to solve this problem, we need to effectively handle the mb_substr function. A common solution is to ensure that the mb_substr function can

See all articles