Write a Ruby program to search items start with specified string of a given array.
arra1 = ['abcde', 'abdf', 'adeab', 'abdgse', 'bdefa', 'bacdef'] print "Original array:\n" print arra1 print "\nSearch items start with 'ab':\n" print arra1.grep(/^ab/) print "\nSearch items start with 'b':\n" print arra1.grep(/^b/)
Output:
Original array: ["abcde", "abdf", "adeab", "abdgse", "bdefa", "bacdef"] Search items start with 'ab': ["abcde", "abdf", "abdgse"] Search items start with 'b': ["bdefa", "bacdef"]
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer