Home  >  Article  >  Web Front-end  >  How to intercept a string in JavaScript

How to intercept a string in JavaScript

清浅
清浅Original
2018-11-28 09:33:513730browse

This article will analyze how to intercept strings in JavaScript. It has certain reference value and I hope it will be helpful to everyone.

There are three ways to intercept strings in JavaScript, namely substring, substr, and split. Next, we will introduce their use in detail in the article.

How to intercept a string in JavaScript

[Recommended course: JavaScript Tutorial

substring(start,stop)

Extract the characters between the two specified subscripts in the string

start: a non-negative integer, refers to the position of the first character of the substring to be extracted in the string, the required elements

stop: a non-negative integer, than The last character of the substring to be extracted is positioned 1 more on the string. It can be written or not. If not, the returned substring will continue to the end of the string.

The length of the string is stop-start

If the parameters start and stop are equal, the method returns an empty string. If start is greater than stop, the method will exchange the two parameters before extracting the substring.

<script type="text/javascript">
var str="Hello world!"
document.write(str.substring(3,9))//从第三个字符开始到第八位
</script>

How to intercept a string in JavaScript


substr(start,length)

Subscript the string from start Start intercepting the specified number of characters

start: The starting subscript of the substring to be intercepted must be a numerical value. If negative, this parameter is the position from the end of the string. In other words, -1 refers to the last character in the string, -2 refers to the second to last character, and so on. You must write

length: the number of characters in the substring, which must be a numerical value. If this parameter is not filled in, the characters from the beginning to the end of the string are returned. If length is 0 or negative, an empty string will be returned

<script type="text/javascript">
var str="Hello world!"
document.write(str.substr(-6,4));//从倒数第六个字符开始,截取四位
</script>

How to intercept a string in JavaScript


split(separator,howmany)

Split a string into a string array

separator: string or regular expression, split the string from the place specified by this parameter. Must be filled in

howmany: refers to the maximum length of the returned array. If this parameter is set, no more substrings will be returned than the array specified by this parameter. If this parameter is not set, the entire string will be split regardless of its length. You can choose

<script type="text/javascript">
var str="Hello world!"
document.write(str.split(""));//将每个字符都分隔开,如果中间有空格则表示已字符串形式分隔
</script>

How to intercept a string in JavaScript

Summary: The above is the entire content of this article. I hope it will be helpful for everyone to learn how to intercept strings.


The above is the detailed content of How to intercept a string in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn