split is a String class method in Ruby which is used to split the given string into an array of substrings based on a pattern specified.
Here the pattern can be a Regular Expression or a string. If pattern is a Regular Expression or a string, str is divided where the pattern matches.
Syntax:
arr = str.split(pattern, limit) public
Parameters: arr is the list, str is the string, pattern is the either string or regExp, and limit is the maximum entries into the array.
Returns: Array of strings based on the parameters.
Example 1:
myArray = "Geeks For Geeks" .split
puts myArray
|
Output:
Geeks
For
Geeks
Example 2:
myArray = "Geeks For Geeks" .split(/ /, 2 )
puts myArray
|
Output:
Geeks
For Geeks
Example 3:
myArray = "geeks geeks" .split( 's' , - 1 )
puts myArray
|
Output:
geek
geek