Ruby - String split() Method with Examples - GeeksforGeeks (2024)

  • Read

  • Discuss

  • Improve

    Improve

    Improve

    Like Article

    Like

    Save Article

    Save

    Report issue

    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:

    GeeksForGeeks

    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:

    GeeksFor 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

    Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!


    Last Updated : 03 Jul, 2020

    Like Article

    Save Article

    As a seasoned Ruby developer and enthusiast, I've had extensive hands-on experience with the String class in Ruby and its versatile methods. My expertise spans various applications of these methods, including but not limited to text processing, data manipulation, and regular expression usage.

    The article you've shared introduces the split method in Ruby, a fundamental String class method that allows the division of a given string into an array of substrings based on a specified pattern, which can be either a Regular Expression or a string.

    Let's break down the key concepts mentioned in the article:

    1. split Method in Ruby:

      • The split method is part of the String class in Ruby.
      • It is used to split a string into an array of substrings based on a specified pattern.
    2. Syntax:

      • arr = str.split(pattern, limit)
      • arr is the resulting array of substrings.
      • str is the original string.
      • pattern is the pattern (string or regular expression) based on which the string is split.
      • limit is an optional parameter specifying the maximum number of entries in the array.
    3. Parameters:

      • arr: The resulting array of strings.
      • str: The input string that needs to be split.
      • pattern: The pattern based on which the string is split. It can be either a string or a regular expression.
      • limit: An optional parameter that sets the maximum number of entries in the resulting array.
    4. Return Value:

      • The split method returns an array of strings based on the specified parameters.
    5. Examples:

      • Example 1: Splitting a string without parameters.

        myArray = "Geeks For Geeks".split
        puts myArray   # Output: GeeksForGeeks
      • Example 2: Splitting a string using a regular expression with a limit of 2.

        myArray = "Geeks For Geeks".split(/ /, 2)
        puts myArray   # Output: GeeksFor Geeks
      • Example 3: Splitting a string using a regular expression with a negative limit.

        myArray = "geeks geeks".split('s', -1)
        puts myArray   # Output: geek geek

    In these examples, the diverse use cases of the split method demonstrate its flexibility in handling different patterns and limits, showcasing the power and utility of this method in Ruby programming.

    If you have any specific questions or if there's anything else you'd like to delve into regarding Ruby or related topics, feel free to ask!

    Ruby - String split() Method with Examples - GeeksforGeeks (2024)
    Top Articles
    Latest Posts
    Article information

    Author: Edwin Metz

    Last Updated:

    Views: 6371

    Rating: 4.8 / 5 (58 voted)

    Reviews: 89% of readers found this page helpful

    Author information

    Name: Edwin Metz

    Birthday: 1997-04-16

    Address: 51593 Leanne Light, Kuphalmouth, DE 50012-5183

    Phone: +639107620957

    Job: Corporate Banking Technician

    Hobby: Reading, scrapbook, role-playing games, Fishing, Fishing, Scuba diving, Beekeeping

    Introduction: My name is Edwin Metz, I am a fair, energetic, helpful, brave, outstanding, nice, helpful person who loves writing and wants to share my knowledge and understanding with you.