How can I split a string into multiple substrings of equal length but from back?
For example if string is: "ABCDEFGH", then I want an array of each string of length 3 as:
["FGH", "CDE", "AB"]
I think that this does what you're asking:
> "ABCDEFGH".reverse.scan(/.{1,3}/).each { |x| x.reverse! }
=> ["FGH", "CDE", "AB"]
Here's a quick explanation:
.reverse reverses the string so that it is "HGFEDCBA" instead of "ABCDEFGH".
.scan(/.{1,3}/) converts the string into an array with each element of the array containing 3 characters (if the string isn't divisible by 3 then the last element of the array may have 1 or 2 characters).
.each { |x| x.reverse! } reverses the characters in each element of the array.
You could define a function like this:
def slice_string_from_end(s)
s.reverse.scan(/.{1,3}/).each { |x| x.reverse! }
end
Then you can use:
slice_string_from_end("ABCDEFGH")
You can accomplish this using each_slice, but you'll need to reverse the string first, and then re-reverse each individual slice:
x = "ABCDEFGH"
x.chars.reverse.each_slice(3).map(&:reverse).map(&:join)
=> ["FGH", "CDE", "AB"]
x.chars).reverse).each_slice(3)).map(&:reverse)).map(&:join))If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With