Q:

Write a Ruby program to search items start with specified string of a given array

0

Write a Ruby program to search items start with specified string of a given array. 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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"]

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now