js string interception function

Copyright notice: reproduced please indicate the source https://blog.csdn.net/qq_41155191/article/details/83033649

1、slice()

Definition and Usage:

slice () method returns the selected elements from the existing array.

slice () method may extract a portion of the string, and return a new string portion is extracted.

Note:  Slice () method does not change the original array.

grammar:

array.slice(startend);

parameter description
start Optional. Choose where to start working. If it is negative, then it provides an array of tail begin to run from the location. That is, the last element refers -1, -2 means penultimate element, and so on.
end Optional. Where the provisions of the end of selection. This parameter is an array index at the end of an array of fragments. If this parameter is not specified, then sliced ​​array contains all of the elements from the start to the end of the array. If this parameter is negative, then it is prescribed begin to run from the end of the array elements.

Example:

- negative values ​​read from the array elements

var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var test = fruits.slice(-3,-1);

Output: Lemon, Apple

- interception string

var str="www.example.com";
document.write(str.slice(4)); // 从第 5 个字符开始截取到末尾
document.write(str.slice(4,10)); // 从第 5 个字符开始截取到第10个字符

Output: runoob.com runoob

2、substring()

Definition and Usage:

substring () method for extracting specified character string intermediary between the two subscripts.

substring () method returns the substring comprises a  start  character at, but not including  the end  character at.

grammar:

string.substring(from, to);

parameter description
from essential. A non-negative integer, a predetermined position of the first character to be extracted in the substring of string Object.
to

Optional. A non-negative integer, to extract the last character of the substring than a position over the string Object.
If omitted, then the substring will always return to the end of the string.

Example:

- extract some characters from a string ::

var str="Hello world!";
document.write(str.substring(3));
document.write(str.substring(3,7));

Output:! Lo world lo w

3、substr()

Definition and Usage:

substr () method to extract the string from  the start  number of the specified character index began.

Tip:  substr () specified by the parameters is a substring of length and start position, so it can replace substring () and slice () is used.
In IE 4, the start value of the parameter is invalid. In this BUG in, start position is specified in the 0th character. In later versions, this BUG has been fixed.
ECMAscript not to standardize the method, therefore against using it.

Note:  substr () method does not change the source string.

grammar:

string.substr(start,length);

parameter description
start essential. To extract the substring starting index. It must be numeric. If it is negative, then the parameter declaration from the end of the string since the beginning of the position. That is, the value of -1 means the last character in the string, -2 means penultimate characters, and so on.
length Optional. The number of characters in the string. It must be numeric. If this parameter is omitted, the return from the start position to the end stringObject string.

Example:

- to extract some characters from a string:

var str="Hello world!";
var n=str.substr(2);
var n=str.substr(2,3);

Output: llo world llo!

Guess you like

Origin blog.csdn.net/qq_41155191/article/details/83033649