The Wayback Machine - https://web.archive.org/web/20240707102207/https://www.geeksforgeeks.org/ruby-string-split-method-with-examples/
Open In App

Ruby – String split() Method with Examples

Last Updated : 03 Jul, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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:




# Ruby program to demonstrate split method
   
# Split without parameters
# Here the pattern is a 
# single whitespace
myArray = "Geeks For Geeks".split
puts myArray


Output:

Geeks
For
Geeks

Example 2:




# Ruby program to demonstrate split method
  
# Here pattern is a regular expression
# limit value is 2
# / / is one white space
myArray = "Geeks For Geeks".split(/ /, 2)
puts myArray


Output:

Geeks
For Geeks

Example 3:




# Ruby program to demonstrate split method
  
# Here the pattern is a regular expression
# limit value is -1
# if the limit is negative there is no 
# limit to the number of fields returned,
# and trailing null fields are not 
# suppressed.
myArray = "geeks geeks".split('s', -1)
puts myArray


Output:

geek
 geek


Similar Reads

Ruby | BigDecimal split() function
BigDecimal#split() : split() is a BigDecimal class method which splits a BigDecimal number into four parts, returned as an array of values. Syntax: BigDecimal.split() Parameter: BigDecimal values Return: - sign - significant_digits - base - exponent Example #1 : # Ruby code for BigDecimal.split() method # loading library require 'bigdecimal' # decl
2 min read
Difference between Ruby and Ruby on Rails
1. Ruby : Ruby is an object-oriented scripting language launched in 1995 and is known as a general-purpose programming language. It was programmed in C programming language. Ruby is a secured programming language and its syntax is similar to Perl and Python. It was developed on the principle of user interface design and it is mainly used to develop
3 min read
Ruby | String inspect Method
inspect is a String class method in Ruby which is used to return a printable version of the given string, surrounded by quote marks, with special characters escaped. Syntax: str.inspect Parameters: Returns: Here, str is the given string. Example 1: # Ruby program to demonstrate # the inspect method # Taking a string and # using the method puts
1 min read
Ruby | String hex Method
hex is a String class method in Ruby which is used to treats the leading characters from the given string as a string of hexadecimal digits (with an optional sign and an optional 0x) and returns the corresponding number. Zero is returned on error. Syntax: str.hex Parameters: Here, str is the given string. Returns: A corresponding number. Example 1:
1 min read
Ruby | String include? Method
include? is a String class method in Ruby which is used to return true if the given string contains the given string or character. Syntax: str.include? Parameters: Here, str is the given string. Returns: true if the given string contains the given string or character otherwise false. Example 1: # Ruby program to demonstrate # the include? method #
1 min read
Ruby | String delete_suffix! Method
delete_suffix! is a String class method in Ruby which is used to return a copy of the given string with trailing suffix deleted or nil if no change was made. Syntax:str.delete_suffix! Parameters: Here, str is the given string. Returns: A copy of the given string with trailing suffix deleted or nil if no change was made. Example 1: C/C++ Code # Ruby
1 min read
Ruby | String dump Method
dump is a String class method in Ruby which is used to generate a version of the given string with all non-printing characters replaced by \nnn notation and all special characters escaped. Syntax: str.dump Parameters: Here, str is the given string. Returns: A new string with all non-printing characters replaced by \nnn notation and all special char
1 min read
Ruby | String each_byte Method
each_byte is a String class method in Ruby which is used to passes each byte in the given string to the given block or returns an enumerator if no block is given. Syntax: str.each_byte {|integer| block } Parameters: Here, str is the given string. Returns: An enumerator. Example 1: # Ruby program to demonstrate # the each_byte method # Taking a stri
1 min read
Ruby | String * Method
String# * is a String class method in Ruby which is used to returns a new String containing integer copies of the receiver. Here, integer must be greater than or equal to 0. Syntax: str * Integer Parameters: Here, str is the required string and integer is the number of copies. Returns: This method returns the String containing integer copies of the
1 min read
Ruby | String + Method
+() is a String class method in Ruby which is used to return a new string that contains the other specified string concatenated to the given string. Syntax: str + other_str Parameters: Here, str and other_str are the strings which are to be concatenated. Returns: Returns the concatenated string. Example 1: #ruby 2.3.1 # Ruby program to demonstrate
1 min read
Article Tags :
three90RightbarBannerImg