Home Web Front-end JS Tutorial The difference between substring(), substr() and slice() in javascript

The difference between substring(), substr() and slice() in javascript

May 16, 2016 pm 03:41 PM
slice() substr() substring()

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”
Copy after login

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));       //”"(空字符串)
Copy after login

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!

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)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months 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)

Use PHP's substr() function to intercept part of the string and add an ellipsis at the end Use PHP's substr() function to intercept part of the string and add an ellipsis at the end Nov 03, 2023 pm 06:06 PM

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 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

Data merging and splitting technology in PHP database connection Data merging and splitting technology in PHP database connection Sep 08, 2023 pm 05:37 PM

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

substr() function in PHP: how to intercept part of a string substr() function in PHP: how to intercept part of a string Nov 03, 2023 am 10:43 AM

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

How to use the SUBSTRING() function in MySQL How to use the SUBSTRING() function in MySQL Jun 02, 2023 pm 11:32 PM

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',

Extract string using substr() function in PHP Extract string using substr() function in PHP Jun 27, 2023 pm 01:26 PM

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

How to reverse string using substring() method in Java? How to reverse string using substring() method in Java? Apr 21, 2023 pm 11:28 PM

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

Application of PHP string processing function substr() Application of PHP string processing function substr() Jun 19, 2023 pm 09:45 PM

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

See all articles