A brief analysis of substring and substr methods in js

  • 2020-10-23 20:02:40
  • OfStack

1. substring method: Used to extract the character substring(start,end) between two specified subscripts.

Start and end positions, indexing from zero

Parameters to describe
start required. 1 non-negative integer specifying the position of the first character of the substring to be extracted in stringObject.
stop optional. 1 non-negative integer, 1 more than the last character of the substring to be extracted in stringObject. If the parameter is omitted, the substring returned will be 1 up to the end of the string.

The return value

1 new string containing the 1 substring of stringObject, the content of which is all characters from start to ES21en-1, with the length of stop minus start.

instructions

The substring returned by the substring method includes characters at start but not end.
If start is equal to end, then this method returns an empty string (that is, a string of length 0).
If start is larger than end, the method swaps the two parameters before extracting the substring.
If start or end is negative, it will be replaced by 0.

2. substr method

Definition and usage

The substr method is used to return 1 substring of a specified length starting at a specified location.

grammar

stringObject.substr(start [, length ])

Parameters to describe
start required. The starting position of the desired substring. The first character in the string has an index of 0.
length optional. The number of characters that should be included in the returned substring.

instructions

If start is negative, then start= str. length+start.
If length is 0 or negative, an empty string is returned.
If this parameter is not specified, the substring continues to the end of stringObject.

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));--------""

alert(str.substr(0));---------------"0123456789"
alert(str.substr(5));---------------"56789"
alert(str.substr(10));--------------""
alert(str.substr(12));--------------""
alert(str.substr(-5));--------------"56789"
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));------------"9"
alert(str.substr(-1,-5));-----------""  

The above is a brief introduction of the methods of substring and substr in js. I hope it will be helpful for your study.


Related articles: