How to convert string to string array in javascript
In JavaScript, you can use the split() method of a string to convert a string into a string array. This method can split the string into a string array according to the specified delimiter; the syntax format is "string .split(separator)", the separator can be empty or null character.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
In JavaScript, you can use the split() method of string to convert a string into a string array.
The split() method is used to split a string into a string array.
Syntax
string.split(separator,limit)
Parameters | Description |
---|---|
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. |
If an empty string ("") is used as a separator, each character in the string will be split.
Return value: a string array.
Example: Convert string to string array
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 split the matching text as the delimiter.
var str="Hello World !"; console.log(str.split(" ")); console.log(str.split(""));var s = "a2b3c4d5e678f12g"; var a = s.split(/\d+/); //把以匹配的数字为分隔符来切分字符串 console.log(a); //返回数组[a,b,c,d,e,f,g] console.log(a.length); //返回数组长度为7
Example:
If the text matched by the regular expression is located at the edge of the string, the split() method also performs the splitting operation and adds a Empty array.
var s = "122a2b3c4d5e678f12g"; var a = s.aplit(/\d+/); console.log(a); console.log(a.length);
If the specified delimiter is not found in the string, an array containing the entire string is returned.
Example:
The split() method supports a second parameter, which is an optional integer used to specify the maximum length of the returned array. . If this parameter is set, the length of the returned array will not be greater than the value specified by this parameter; if this parameter is not set, the entire string will be split without considering the array length.
var s = "JavaScript"; var a = s.split("", 4); //按顺序从左到右,仅分切4个元素的数组 console.log(a); //返回数组[J,a,v,a] console.log(a.length); //返回值为4
【Related recommendations: javascript learning tutorial】
The above is the detailed content of How to convert string to string array in javascript. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The SPLIT() function splits a string into an array by a specified delimiter, returning a string array where each element is a delimiter-separated portion of the original string. Usage includes: splitting a comma-separated list of values into an array, extracting filenames from paths, and splitting email addresses into usernames and domains.

Ways to sort strings in Java: Use the Arrays.sort() method to sort an array of strings in ascending order. Use the Collections.sort() method to sort a list of strings in ascending order. Use the Comparator interface for custom sorting of strings.

In C language, \0 is the end mark of a string, called the null character or terminator. Since strings are stored in memory as byte arrays, the compiler recognizes the end of the string via \0, ensuring that strings are handled correctly. \0 How it works: The compiler stops reading characters when it encounters \0, and subsequent characters are ignored. \0 itself does not occupy storage space. Benefits include reliable string handling, improved efficiency (no need to scan the entire array to find the end), and ease of comparison and manipulation.

args stands for command line arguments in Java and is an array of strings containing the list of arguments passed to the program when it is started. It is only available in the main method, and its default value is an empty array, with each parameter accessible by index. args is used to receive and process command line arguments to configure or provide input data when a program starts.

args is a special parameter array of the main method in Java, used to obtain a string array of command line parameters or external input. By accessing the args array, the program can read these arguments and process them as needed.

Detailed explanation of the method of converting int type to string in PHP In PHP development, we often encounter the need to convert int type to string type. This conversion can be achieved in a variety of ways. This article will introduce several common methods in detail, with specific code examples to help readers better understand. 1. Use PHP’s built-in function strval(). PHP provides a built-in function strval() that can convert variables of different types into string types. When we need to convert int type to string type,

AI technology has been combined with PHP functions to enhance the functionality of the application. Specific AI applications include: using machine learning algorithms to classify text, such as Naive Bayes. Perform in-depth text analysis using natural language processing techniques such as word segmentation and stemming.

1. First open pycharm and enter the pycharm homepage. 2. Then create a new python script, right-click - click new - click pythonfile. 3. Enter a string, code: s="-". 4. Then you need to repeat the symbols in the string 20 times, code: s1=s*20. 5. Enter the print output code, code: print(s1). 6. Finally run the script and you will see our return value at the bottom: - repeated 20 times.
