Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to convert a string to a string array by javascript

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how javascript converts strings into string arrays". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to convert strings into string arrays by javascript" can help you solve the problem.

In javascript, you can use the split () method of a string to convert a string into an array of strings, which splits a string into an array of strings based on a specified delimiter; the syntax format is "string.split", which can be an empty or empty character.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

In javascript, you can use the split () method of a string to convert a string to a string array.

The split () method is used to split a string into an array of strings.

Grammar

The string.split (separator,limit) parameter description separator is optional. A string or regular expression that splits the string Object from the place specified by the parameter. Limit is 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 is split, regardless of its length.

If an empty string ("") is used as a separator, each character in the string will be split.

Return value: an array of strings.

Example: convert a string to an array of strings

Var str= "Hello World!"; console.log (str.split ("")); console.log (str.split (""))

Output:

Example: if the parameter is a regular expression, the split () method can be segmented with matching text as the delimiter.

Var str= "Hello World!"; console.log (str.split (")); console.log (str.split (")); var s = "a2b3c4d5e678f12g"; var a = s.split (/\ dline /); / / split the string console.log (a) with a matching number as a delimiter; / / return the array [console.log (a.length)]; / / return the array length is 7

Example:

If the text matched by the regular expression is on the edge of the string, the split () method also performs the sharding operation and adds an empty array to the array.

Var s = "122a2b3c4d5e678f12g"; var a = s.aplit (/\ dcats /); console.log (a); console.log (a.length)

If the delimiter specified in the string is not found, an array containing the entire string is returned.

Example:

The split () method supports the second parameter, which is an optional integer that specifies the maximum length of the returned array. If this parameter is set, the returned array length will not be greater than the value specified by this parameter; if this parameter is not set, the entire string is split and the array length is not considered.

Var s = "JavaScript"; var a = s.split (", 4); / / from left to right, only splits the array of 4 elements console.log (a); / / returns the array [JSCH _ a _ a] console.log (a.length); / / returns a value of 4 on" how javascript converts strings into string arrays ". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report