Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does JS intercept strings

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to intercept strings in JS". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to intercept strings by JS.

Substr () method

The substr () method extracts a specified number of characters from a string starting with the start subscript.

StringObject.substr (start,length)

Start: required. The starting subscript of the substring to be extracted. Must be a numeric value. If it is a negative number, 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 penultimate 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 the stringObject is returned.

Remember: the second parameter of substr () is the length

Var str= "abcdefg"; str.substr (3) / / defgstr.substr (3jue 2) / / de (2 indicates the length, the subscript begins with 3, including the two characters after 3) str.substr (- 2) / / fg 2 the string str.substr (- 3p2) / / ef from the penultimate character to the end starts the substring () method

The substring () method is used to extract characters that are mediated between two specified subscripts.

StringObject.substring (start,stop)

Start: required. A non-negative integer that specifies the position of the first character of the substring to be extracted in the stringObject.

Stop: optional. A non-negative integer that is 1 more positioned in stringObject than the last character of the substring to be extracted.

If this parameter is omitted, the substring returned will go all the way to the end of the string.

Return value: a new string containing a substring of stringObject whose contents are all characters from start to stop-1, whose length is stop minus start.

Note: the substring returned by the substring () method includes the characters at start, but not the characters at stop.

If the parameter start is equal to stop, the method returns an empty string (that is, a string of length 0). If start is larger than stop, the method swaps these two parameters before extracting the substring.

Var str= "abcdefg" str.substring (3) / / defgstr.substring (3pyr3) / / empty string str.substring (3p5) / / de the subscript begins with 3 (including 3), and the string str.substring (5p3) / / de with the following table of 5 (excluding 5) will exchange two parameters when the first parameter is > the second parameter. Change to: str.substring (3pj5) str.substring (- 3) / / abcdefg substring () does not accept negative arguments. If the argument is negative, the original string slice () method is returned.

The slice () method returns the selected element from an existing array.

ArrayObject.slice (start,end)

Start: required. Specify where to start the selection. If it is negative, it specifies the position from the end of the array. That is,-1 refers to the last element,-2 refers to the penultimate element, and so on.

End: optional. Specifies where to end the selection. This parameter is the array subscript at the end of the array fragment. If this parameter is not specified, the shredded array contains all the elements from start to the end of the array. If this parameter is negative, it specifies the element starting at the end of the array.

The negative value parameter of var str= "abcdefg" str.slice (3) / / defg str.slice (3Magi 5) / / destr.slice (- 2) / / fgstr.slice (3Laver 2) / / destr.slice (- 4mae2) / / de starts from the following, and-1 represents the last element str.slice (3mam 6) / / empty array, when the element index specified by the second parameter is less than the element index specified by the first hotel. Return an empty array / / if it is an array, then return a new array var arr = ['axiaxiaoyangzhongjingpai] str.slice (3) / / [' dwelling pencils'] str.slice (3) / / ['dwelling pencils']. I believe you all have a better understanding of "how to intercept strings by JS". You might as well do it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report