To get all possible substrings of a string in Ruby, you can use nested loops to iterate through each combination of start and end indices.
In this example,
str
with a value of 'Hello'
.substrings
to store all substrings.substrings
array.substrings
array.str = 'Hello'
substrings = []
(0...str.length).each do |i|
(i + 1..str.length).each do |j|
substrings << str[i...j]
end
end
puts 'All substrings:', substrings
All substrings: H He Hel Hell Hello e el ell ello l ll llo l lo o
In this tutorial, we learned How to get all the possible Substrings of a String in Ruby language with well detailed examples.