Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use split in javascript

Shulou Source: shulou.com Published: 2022-06-03 05:21:22 09月20日 Update

This article mainly introduces how to use split in javascript. It is very detailed and has a certain reference value. Friends who are interested must finish it!

The function of javascript split is to split a string into an array of strings, and its syntax is "stringObject.split (separator,howmany)".

This article operating environment: windows7 system, javascript1.8.5 version, Dell G3 computer.

What is the use of javascript split?

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

Grammar

StringObject.split (separator,howmany)

Parameters.

Separator is required. A string or regular expression that splits the stringObject from the place specified by the parameter.

Howmany 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.

Return value

An array of strings. The array is created by splitting the string stringObject into substrings at the boundary specified by separator. The string in the returned array does not include separator itself.

However, if separator is a regular expression that contains subexpressions, the returned array includes strings that match those subexpressions (but not text that matches the entire regular expression).

Tips and comments

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

Note: the action performed by String.split () is the opposite of that performed by Array.join.

Example

Example 1

In this example, we will split the string in different ways:

Var str= "How are you doing today?" [xss_clean] (str.split ("") + ") [xss_clean] (str.split (") + ") [xss_clean] (str.split (", 3))

Output:

How,are,you,doing,today?H,o,w, a,r,e, y,o,u, d,o,i,n,g, t,o,d,a,y,?How,are,you

Example 2

In this example, we will split a string with a more complex structure:

"2JV 3VL 4RV 5" .split (":") / / will return ["2", "3", "4", "5"] "| a | b | c" .split ("|") / / will return ["", "a", "b", "c"]

Example 3

Using the following code, you can divide a sentence into words:

Var words = sentence.split ('')

Or use a regular expression as the separator:

Var words = sentence.split (/\ swords /)

Example 4

If you want to split a word into letters, or a string into characters, use the following code:

"hello" .split (") / / can return [" h "," e "," l "," l "," o "]

To return only a portion of the characters, use the howmany parameter:

"hello" .split (", 3) / / return [" h "," e "," l "] these are all the contents of the article" how to use split in javascript ". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Characters strings arrays parameters expressions examples regularities comments code content words articles grammar length facets differences complexity maximum between value Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Microsoft MariaDB macOS OPPO Reno