The string is just a simple text or characters which can include the alphabets, numbers, or symbols.
Let’s have a look at the syntax of the split method.
Here, the separator could be a single character using which you would like to split the string.
And the limit is the limit of splits. When the number of substrings becomes equal to the limit, the split() method stops.
Let’s dive in and get a couple of examples done for the understanding of the split() function.
We suppose a string “Linuxhint is great and working hard to grow.”. Now, let’s try to split the string into arrays of string using the split() method. By providing the “ ” space character as a separator. But, without providing the limit. Later, we will do it with the limit.
As you can see that the split() method has returned the array of substrings, based on the “ ” space.
Now, let’s see what happened if we provide a limit as well.
As you can see that the split() function stopped splitting the string, and it would stop splitting the string when the count is equivalent to the number of substrings.
Now, let’s see if we don’t provide a separator just an empty string, and calls the split() function.
As you can see that the function has split and returned the array of each character separated.
Now, let’s see if we don’t provide both of the arguments and just calls the split() function.
Great, the split() function has returned an array with a single substring in it that is the whole string
What if we want to split the string on the base of two separators or we want separators as well in the output array of substring? Luckily, there is a solution as well, We can provide regular expressions as a separator as well.
So, let’s see how can we split the string with two separators. Space “ “ character and “i” character
Alright! It worked great. As we expect it to split.
Now, what if we want to include the separators as well in the array of the substring. We will simply add parenthesis() around the regular expression.
Perfect, as you can see that the separators are also included in the array of substrings.