Home  >  Article  >  Web Front-end  >  How to cut strings and convert types in es6

How to cut strings and convert types in es6

青灯夜游
青灯夜游Original
2023-02-13 12:05:172506browse

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.

How to cut strings and convert types in es6

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);

How to cut strings and convert types in es6

Example 2:

var str="Welcome to here !";
var n=str.split(" ");
console.log(n);

How to cut strings and convert types in es6

Example 3:

var str="Welcome to here !";
var n=str.split("e");
console.log(n);

How to cut strings and convert types in es6

[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!

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