How to intercept specified content in js string
This article mainly introduces "how the js string intercepts the specified content". In the daily operation, I believe that many people have doubts about how to intercept the specified content in the js string. The editor consulted all kinds of data and sorted out a simple and easy-to-use method of operation. I hope it will be helpful to answer the doubt of "how to intercept the specified content in the js string". Next, please follow the editor to study!
Method 1: use the slice () method to segment and intercept the specified string and store it in an array
Extracts a part of the string with the specified start and end positions and returns the extracted part as a new string.
Var a = "i am a boy"; var b=a.slice (0recom 6); / / "i am a"
Method 2: use the substring () method to intercept the specified string
Used to extract characters that are mediated between two specified subscripts.
Var str = 'abcdefghij';console.log (' (1,2):'+ str.substring (1,2)); / /'(1,2): b'console.log ('(1,1):'+ str.substring (1,1)); / /'(1,1):'
Method 3: use the substr () method to intercept the specified string
Returns a substring of the specified length starting at the specified location.
Var str = 'abcdefghij';console.log (' (1,2):'+ str.substr (1,2)); / /'(1,2): bc'console.log ('(- 3,2):'+ str.substr (- 3,2)); / /'(- 3,2): hi' ends the study of "how js strings intercept specified content", hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!