However, in case you are here only for the solution use this link.
Table of Contents
- Converting strings to array using split
- Limitations & Caveats
- Other Related Concepts
Once you have understood how strings are converted to array using
splitplease feel free to explore the other methods as well.
Converting string to array using split:As the name suggests, the
" "or even a
Syntax of split:
stringrefers to the original string that we are looking to convert.
delimiter- Optional, the character to be used to split the string. In case it is left empty the entire character in the string is returned in an array.
limit- Optional, an integer value indicating how many times the string needs to be split.
Code & Explanation:
As you could see, we have passed a
,is a delimiter.
And as mentioned above an array with each character of the string is returned. Next, let's take a look at an example using the
As you can see, the items after the limit were not split. However, keep in mind that the limit does not refer to the index but rather the 4th occurrence of the delimiter.
- If no delimiter is passed the entire string is returned as one element in an array and if an empty string
""is passed, the string splits each character and an array.