I want to reverse a string each two characters with Ruby.
The input:
"0123456789abcdef"
The output that I expect:
"efcdab8967452301"
I'm not sure if this is the best way to do it:
"0123456789abcdef".scan(/../).reverse.join == "efcdab8967452301"
"0123456789abcdef".gsub(/../, &:reverse).reverse
# => "efcdab8967452301"
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