Home >Web Front-end >Front-end Q&A >How to cut strings and convert types in es6
In es6, you can use the split() function to cut the string and convert the type, the syntax is "str.split (separator, maximum length of the array)". The split() function can convert a string into an array type. It uses the specified separator provided in the parameter to cut the string into a substring and use the substring array as an element. The second parameter of the split() function is used to specify the maximum length of the returned array and can be omitted; if this parameter is set, the returned substring will not be more than the array specified by this parameter.
The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.
In es6, you can use the split() function to split a string and convert it into an array type.
split() function introduction
The split() method is used to split the given string into a string array; this method is Use the specified delimiter provided in the parameter to cut the string into a substring, and then pass it into the array as elements one by one.
Syntax:
str.split(separator, limit)
Parameters:
separator: Optional. A string or regular expression to split the string Object from where specified by this parameter.
#limit: Optional. This parameter specifies 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.
Example 1:
var str="Welcome to here !"; var n=str.split(""); console.log(n);
Example 2:
var str="Welcome to here !"; var n=str.split(" "); console.log(n);
Example 3:
var str="Welcome to here !"; var n=str.split("e"); console.log(n);
[Recommended learning: javascript advanced tutorial]
The above is the detailed content of How to cut strings and convert types in es6. For more information, please follow other related articles on the PHP Chinese website!