


The difference between substring(), substr() and slice() in javascript
There are three commonly used character interception functions in js: slice(), substring(), and substr(). Let’s introduce to you the functions of slice(), substring(), and substr() in character interception. Some usages and differences.
stringObject.substring(start,stop)
is used to extract the string between two Specifies the characters between subscripts.
start is required. A nonnegative integer that specifies the position in stringObject of the first character of the substring to be extracted.
stop is optional. A nonnegative integer that is one position in the stringObject that is one more than the last character of the substring to be extracted. If this parameter is omitted, the returned substring will go to the end of the string.
start starts from 0 and ends with stop (not including stop). Negative parameters are not accepted.
stringObject.substr(start,length)
Can extract the specified number of characters starting from the start subscript in the string
start Required. The starting index of the substring to be extracted. Must be a numeric value. If negative, this parameter declares the position from the end of the string. That is, -1 refers to the last character in the string, -2 refers to the second to last character, and so on.
length optional. The number of characters in the substring. Must be a numeric value. If this parameter is omitted, the string from the beginning to the end of stringObject is returned.
stringObject.slice(start,end)
Extract a certain part of the string and return the extracted part as a new string Section
start The starting index of the segment to be extracted. If it is a negative number, this parameter specifies the position starting from the end of the string. That is, -1 refers to the last character of the string, -2 refers to the second to last character, and so on.
end The index immediately following the end of the segment to be extracted. If this parameter is not specified, the substring to be extracted includes the string from start to the end of the original string. If this parameter is negative, it specifies the position from the end of the string.
Returns the new string including all the characters of stringObject from start (including start) to end (excluding end)
string.slice() string.substring() string.substr() var stringValue = “hello world”; alert(stringValue.slice(3)); //”lo world” alert(stringValue.substring(3)); //”lo world” alert(stringValue.substr(3)); //”lo world” alert(stringValue.slice(3,7)); //”lo w” alert(stringValue.substring(3,7)); //”lo w” alert(stringValue.substr(3,7)); //”lo worl”
If there is only one parameter n among the three, the remaining string will be returned starting from the nth position (the position is calculated from 0)
If there are two parameters n, m, slice and substring will Returns the string starting at the n-th position and ending at the m-th position (not including the m-th position), while substr will return m characters starting at the n-th position.
string.slice() string.substring() string.substr() var stringValue = “hello world”; alert(stringValue.slice(-3)); //”rld” alert(stringValue.substring(-3)); //”hello world” alert(stringValue.substr(-3)); //”rld” alert(stringValue.slice(3,-4)); //”lo w” alert(stringValue.substring(3,-4)); //”hel” alert(stringValue.substr(3,-4)); //”"(空字符串)
When the parameter is a negative value, slice will add the negative value to the string length (string.length), and substr will add the negative value One argument plus the length of the string, the second converted to 0, substring will convert all negative values to 0.
IE's JavaScript implementation has a problem when handling negative values passed to the substr() method, and it returns the original string.
The above is the entire content of this chapter. For more related tutorials, please visit JavaScript Video Tutorial!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Use PHP's substr() function to intercept part of the string and add an ellipsis at the end. In actual development, we often encounter situations where we need to intercept a string. PHP's substr() function is a very commonly used string interception function. This article will use a practical example to demonstrate how to use the substr() function to intercept part of a string and add an ellipsis at the end. First, I'll provide a sample code. Then I'll explain this code. <?phpfuncti

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

Data merging and splitting technology in PHP database connection In Web application development, database connection is a very important part. As a very commonly used server-side scripting language, PHP provides a wealth of database connection extensions. This article will explore how to use PHP to connect to a database and introduce data merging and splitting techniques. Connecting to the database In PHP, by using some specific database connection extensions, we can easily connect to various types of databases, including MySQL, Oracle, SQLite, etc. this

The substr() function in PHP: How to intercept part of a string requires specific code examples. In PHP programming, string processing is one of the most common operations. Intercepting part of a string is a requirement that is often encountered when processing strings. In PHP, we can use the built-in substr() function to intercept part of a string. This article will introduce the usage of substr() function in detail and give specific code examples. The basic usage of the substr() function is as follows: string

SUBSTRING()SUBSTRING(str,pos), SUBSTRING(strFROMpos), SUBSTRING(str,pos,len) and SUBSTRING(strFROMposFORlen) functions can all be used to return the substring starting from the specified position pos. len represents the length of the returned substring. ; pos is 0 means returning an empty string. For example: SELECTSUBSTRING('MySQL string function',-2)ASstr1,SUBSTRING('MySQL string function',

Extracting strings using substr() function in PHP When developing web applications, string processing takes up a large part of the time and effort. PHP provides a wealth of string processing functions, one of which is very commonly used function is substr(). This function can be used to extract characters or strings of a specified length from a string. The basic syntax of the substr() function is very simple: substr(string$string,int$start,int

使用substring()方法packagenet.javaguides.corejava.string;/****@authorRameshFadatare**/publicclassUsingSubStringFunction{//FunctiontoreverseastringinJavausingrecursionprivatestaticStringreverse(Stringstr){//basecase:ifstringisnulloremptyif(str==null||str.

PHP is a programming language widely used in web development. It has many string processing functions, one of which is widely recognized as the substr() function. This article will delve into the application and use of the substr() function to help you better process strings. 1. Introduction to substr() function The substr() function is used to intercept a part of characters from a string. Its syntax is: substr(string$string,int
