JavaScript’s split() method splits a string into substrings, which are then saved in a new array. This function will yield a new variety as a result. This approach preserves the original string without changing anything.
Syntax:
- string. split()
- string. split(separator)
- string. split(separator, limit)
Both the limit and the separator are optional parameters.
Separator: The string is split at each instance where the designated separator appears.
Limit: The string is split into the specified number of segments by this non-negative integer.
Examples:
var str=”Enjoy coding while learning it”
- var a = str. split() → [‘Enjoy coding while learning it’]
⇒ Here, we are calling split on the string str without any parameters by assigning this to a. In that case, it does not have any separator so there is no split will happen because we didn’t pass anything to our method, it is just going to return an array.
- var b = str. split(‘ ‘) → [‘Enjoy’, ‘coding’, ‘while’, ‘learning’, ‘it’]
⇒ Here, we are calling split on the string str with a parameter that’s going to be a separator by assigning this to b. In that case, it has a separator so a split will happen because we pass in space as the separator to our method. So, on each space, the split occurs and then returns an array of strings.
- var c = str. split(”) → [‘E’, ‘n’, ‘j’, ‘o’, ‘y’, ‘ ‘, ‘c’, ‘o’, ‘d’, ‘i’, ‘n’, ‘g’, ‘ ‘, ‘w’, ‘h’, ‘i’, ‘l’, ‘e’, ‘ ‘, ‘l’, ‘e’, ‘a’, ‘r’, ‘n’, ‘i’, ‘n’, ‘g’, ‘ ‘, ‘i’, ‘t’]
⇒ Here, we are calling split on the string str with a parameter that’s going to be a separator by assigning this to c. In that case, it has a separator so a split will happen because we pass in an empty string as the separator to our method. So, the split occurs and then returns all the characters separated inside an array.
- var d = str. split(‘n’) → [‘E’, ‘joy codi’, ‘g while lear’, ‘i’, ‘g it’]
⇒ Here, we are calling split on the string str with a parameter that’s going to be a separator by assigning this to d. In that case, it has a separator so a split will happen because we pass in a n as the separator to our method. So, the split occurs and then returns an array of substrings. Here, ‘n’ is not included in the substrings because the separator is not included.
- var e = str. split(‘n’, 2) → [‘E’, ‘joy codi’]
⇒ Here, we are calling split on the string str with parameters that are going to be a separator and limit by assigning this to e. In that case, it has a separator and limit so a split will happen based on the limit because we pass in a n as the separator and 2 as the limit to our method. So, the split occurs and then returns the first two splits as an array of substrings.
Code 1:
<html> <head> <title> Split() Method </title> </head> <body> <script> var str= "This is a beautiful world"; document. write(str. split(" ")); </script> </body> </html>
Output:
This, is, a, beautiful, world
In this code 1, the split() function splits the string str wherever the whitespaces (” “) occurs and returns an array of strings. Here, we are using only a separator.
Code 2:
<html> <head> <title> Split() Method </title> </head> <body> <script> var str= "This is a beautiful world"; var arr = str. split(" "); document. write(arr[3]); </script> </body> </html>
Output:
beautiful
In this code 2, the split() function splits the string str based on the index and returns an array of strings.
You may also learn,