How to use the "split" method in Ruby

Source: Internet
Author: User

As you know, ruby strings are called first-class objects and use a large number of query and operation methods. A basic string operation is to split a string into multiple sub-strings. If you have a string, such as "foo, bar, baz", and you want the three strings "foo", "bar", and "baz ". The split method of the string class can help you.
Basic usage of "split"
The basic usage of the split method is to split a string consisting of a static sequence of individual characters or characters. If the first variable of split is a string, the character in this string will be used as a string separator. However, in data separated by commas (,), commas (,) are used to separate data. Each string component is used to separate data.

#!/usr/bin/env rubystr = "foo,bar,baz"puts str.split(",")$ ./1.rbfoobarbaz

Use regular expressions to increase flexibility

Of course, there are some easier ways to separate strings. Regular Expressions can increase flexibility for the split method. Use "foo, bar, baz" again ". The interval is separated by the first comma instead of the second comma. If the string "," is treated as a separator, there will still be an interval at the beginning of the "baz" string. If the "," string is used, it only matches with the first comma, because there is no interval after the second comma. Its function is still limited.

The solution to this problem is to use a regular expression as the independent variable of the delimiter instead of a string. A regular expression can not only match characters in a static sequence, but also play a role in non-quantitative and optional characters.

Write Regular Expressions

Write a regular expression for your separator. The first step is to clearly describe what the separator is. In this case, it is reasonable to have one or more spaces after a comma. There are two elements in the Regular Expression Library: comma and optional interval. The asterisk (*) quantizer is used for the interval. It indicates "zero or more ". Any element before it matches zero or several times. For example, the regular expression/a */matches the sequence with zero or more "a" characters.

#!/usr/bin/env rubystr = "foo, bar,baz"puts str.split( /, */ )$ ./2.rbfoobarbaz

Limit the number of splits

Imagine a comma that separates the strings "10, 20, 30, this is an arbitary string ". The format is three numbers followed by a comment. This comment can contain any text, including text with commas. To prevent the split text, we can set a maximum number of split columns. Note that the comment string of any text is used only when it is in the last column of the table.

To limit the number of splits, the number of Number Fields in the string must be sent as the second variable of the split method.

#!/usr/bin/env rubystr = "10,20,30,ten, twenty and thirty"puts str.split( /, */, 4 )$ ./3.rb102030en, twenty and thirty

Understand Its Limitations

The splitting method has some limitations. Take the string "10, 20, 'Bob, eve and mallory ', 30" as an example. The expected character is two numbers, followed by a quote string (which may contain commas) and followed by another number. Split cannot correctly separate the fields of this string. To ensure correct separation, you need to keep the string scanner in the Enabled state so that it will remember whether it is inside the quote string. If the scanner is not started, the problem cannot be solved.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.