DescriptionThis method splits a String object into an array of strings by separating the string into substrings. Show
SyntaxIts syntax is as follows − string.split([separator][, limit]); Argument Details
Return ValueThe split method returns the new array. Also, when the string is empty, split returns an array containing one empty string, rather than an empty array. ExampleTry the following example. <html> <head> <title>JavaScript String split() Method</title> </head> <body> <script type = "text/javascript"> var str = "Apples are round, and apples are juicy."; var splitted = str.split(" ", 3); document.write( splitted ); </script> </body> </html> OutputApples,are,round, javascript_strings_object.htm In this tutorial, we will learn about the JavaScript String split() method with the help of examples. The Example
split() SyntaxThe syntax of
Here, split() ParameterThe
split() Return Value
Note: The Example: Using split()
Output [ 'A', 'B', 'C', 'D', 'E', 'F' ] [ 'Java is awesome', ' Java is fun', '' ] [ 'Java is awesome', ' Java is fun' ] [ 'JavaScript ', ' Python ', 'C', 'C++' ] [ 'JavaScript', 'Python', 'C', 'C++' ] Note: If the separator is a regular expression with capturing parentheses, then each time the separator matches, the results of the capturing parentheses are spliced into the output array. Recommended Reading: JavaScript Array join() If you need to split up a string into an array of substrings, then you can use the JavaScript In this article, I will go over the JavaScript Basic Syntax of the split() methodHere is the syntax for the JavaScript
The optional separator is a type of pattern that tells the computer where each split should happen. The optional limit parameter is a positive number that tells the computer how many substrings should be in the returned array value. ADVERTISEMENT JavaScript split() method code examplesIn this first example, I have the string
Examples using the optional separator parameterIf I wanted to change it so the string is split up into individual characters, then I would need to add a separator. The separator would be an empty string.
Notice how the spaces are considered characters in the return value. If I wanted to change it so the string is split up into individual words, then the separator would be an empty string with a space.
ADVERTISEMENT Examples using the optional limit parameterIn this example, I am going to use the limit parameter to return an array of just the first word of the
sentence
If I change the limit to be zero, then the return value would be an empty array.
Should you use the split() method to reverse a string?The reverse a string exercise is a very popular coding challenge. One common way to solve it involves using the In this example, we have the string "freeCodeCamp". If we wanted to reverse the word, then we can chain
together the
The The The This approach seems to work fine for this example. But there are special cases where this would not work. Let's take a look at the example provided in the MDN documentation. If we tried to reverse the string "mañana mañana", then it would lead to unexpected results.
Notice how the tilde(~) is placed over the letter A grapheme cluster is a series of symbols combined to produce one single character that humans can read on screen. When we try to reverse the string using these types of characters, the computer can misinterpret these characters and produce an incorrect version of the reversed string. If we just isolate the split method, you can see how the computer is breaking up each individual character.
There are packages that you can use in your projects to fix this issue and reverse the string correctly if you are using these special characters. ADVERTISEMENT ConclusionThe JavaScript Here is syntax for the JavaScript
The optional separator is a type of pattern that tells the computer where each split should happen. The optional limit parameter is a positive number that tells the computer how many substrings should be in the returned array value. You could use the split method to reverse a string, but there are special cases where this would not work. If your string contains grapheme clusters, then the result might produce an incorrectly reversed word. You could also choose to use the spread syntax to split up the string before reversing it.
I hope you enjoyed this article and best of luck on your JavaScript journey. Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started |