Home > Web Front-end > JS Tutorial > Getting started with the split method of JavaScript string objects (used to split strings into arrays)

Getting started with the split method of JavaScript string objects (used to split strings into arrays)

PHPz
Release: 2018-09-30 14:42:41
Original
1872 people have browsed it

This article mainly introduces an introductory example of the split method of JavaScript string objects. The split method is used to split a string into an array. Friends in need can refer to

JavaScript split method The

split method is used to split a string into a string array and return the array. The syntax is as follows:

str_object.split(separator, limit)
Copy after login

Parameter description:

参数说明
str_object要操作的字符串(对象)
separator必需。分隔符,字符串或正则表达式,从该参数指定的地方分割 str_object
limit可选。指定返回的数组的最大长度。如果设置了该参数,返回的子串不会多于这个参数指定的数组。如果省略该参数,则符合规则都将被分割

Tip: If an empty string ("") is used as a separator, each character in str_object will be separated by Split, as shown in the example below.

split method instance

<script language="JavaScript">
var str = "www.php.cn";
document.write( str.split(".") + "<br />" );
document.write( str.split("") + "<br />" );
document.write(str.split(".", 2));
</script>
Copy after login

Run this example, the output is:

www,php,cn
w,w,w, .,p,h,p,.,c,n
www,php

Tip: As shown in the above example, if the empty string ("") is used as separator, str_object will be separated between each character.

split method uses regular expressions

The split method also supports using regular expressions to split strings:

<script language="JavaScript">
document.write( "1a2b3c".split(/\d/) + "<br />");
document.write( ":a:b:c".split(":") );
</script>
Copy after login

Run the example , Output:

a,b,c
,a,b,c

Please carefully observe the difference in the output of the two examples.

Summary: The above is the entire content of this article, I hope it will be helpful to everyone’s study. For more related tutorials, please visit JavaScript Video Tutorial!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template