Detailed explanation of substring and substr methods in JS
1. substring method
Definition and usage:
The substring method is used to extract a string between two specified subscripts. characters between.
Syntax:
stringObject.substring(start,end)
Parameter Description:
start Required. A nonnegative integer that specifies the position in stringObject of the first character of the substring to be extracted.
end 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.
Return value:
A new string value contains a substring of stringObject, whose content is all characters from start to end-1 , its length is end minus start.
Description: The substring returned by the
substring method includes the characters at start, but does not include the characters at end.
If start and end are equal, then this method returns an empty string (that is, a string with a length of 0).
If start is greater than end, then this method will exchange the two parameters before extracting the substring.
If start or end is negative, then it will be replaced with 0.
Example:
var str = "0123456789";
alert(str.substring(0));------------"0123456789"
alert(str.substring(5)) ;------------"56789"
alert(str.substring(10));----------""
alert(str.substring (12));-----------""
alert(str.substring(-5));-----------"0123456789"
alert (str.substring(-10));----------"0123456789"
alert(str.substring(-12));----------"0123456789"
alert(str.substring(0,5));----------"01234"
alert(str.substring(0,10));------- --"0123456789"
alert(str.substring(0,12));---------"0123456789"
alert(str.substring(2,0));--- -------"01"
alert(str.substring(2,2));----------""
alert(str.substring(2,5) );----------"234"
alert(str.substring(2,12));----------"23456789"
alert(str.substring (2,-2));---------"01"
alert(str.substring(-1,5));---------"01234"
alert(str.substring(-1,-5));--------""
2. substr method
Definition and usage:
The substr method is used to return a substring of the specified length starting from the specified position.
Syntax:
stringObject.substr(start [, length])
Parameter Description:
start Required. The starting position of the desired substring. The first character in the string has index 0.
length Optional. The number of characters that should be included in the returned substring.
Description:
If length is 0 or a negative number, an empty string will be returned.
If this parameter is not specified, the substring will continue to the end of stringObject.
Example:
var str = "0123456789";
##alert(str.substr(0) );---------------"0123456789"alert(str.substr(5));---------------"56789 "
alert(str.substr(10));--------------""
alert(str.substr(12));------- -------""
alert(str.substr(-5));--------------"0123456789"
alert(str.substr(-- 10));-------------"0123456789"
alert(str.substr(-12));-------------"0123456789"
alert(str.substr(0,5));-------------"01234"
alert(str.substr(0,10));---- --------"0123456789"
alert(str.substr(0,12));------------"0123456789"
alert(str.substr( 2,0));-------------""
alert(str.substr(2,2));-------------" 23"
alert(str.substr(2,5));-------------"23456"
alert(str.substr(2,12));-- ----------"23456789"
alert(str.substr(2,-2));------------""
alert(str. substr(-1,5));----------------"01234"
alert(str.substr(-1,-5));--------- --""
The above is the detailed content of Detailed explanation of substring and substr methods in JS. For more information, please follow other related articles on the PHP Chinese website!

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

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

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





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

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

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

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

This article will explain in detail how PHP converts the first letter of a string to lowercase. I think it is very practical, so I share it with you as a reference. I hope you can gain something after reading this article. Converting the first letter of a PHP string to lowercase Introduction In PHP, converting the first letter of a string to lowercase is a common operation. This can be achieved by using the built-in function lcfirst() or the string operator strtolower(). This guide will dive into both approaches, providing example code and best practices. Method 1: Use the lcfirst() function The lcfirst() function is specifically designed to convert the first letter of a string to lowercase while leaving the rest of the characters unchanged. Its syntax is as follows: st
