Reverse a string each two characters with Ruby

微笑、不失礼 提交于 2019-12-02 02:20:37

问题


I want to reverse a string each two characters with Ruby.

The input:

"0123456789abcdef"

The output that I expect:

"efcdab8967452301"

回答1:


I'm not sure if this is the best way to do it:

"0123456789abcdef".scan(/../).reverse.join == "efcdab8967452301"



回答2:


"0123456789abcdef".gsub(/../, &:reverse).reverse
# => "efcdab8967452301"



回答3:


You can try:

"0123456789abcdef".chars.each_slice(2).to_a.reverse.join



回答4:


I like all the solutions, especially @sawa's, but now, six hours later, its a challenge to come up with something different. In the interest of diversity, I offer a way that uses pairwise substitution. It's destructive, so I start by making a copy of the string.

Code

str = "0123456789abcdef"
s = str.dup
(0...s.size/2).step(2) { |i| s[i,2], s[-i-2,2] = s[-i-2,2], s[i,2] }
  s #=> "efcdab8967452301"

Explanation

Letting

enum = (0...s.size/2).step(2) #=> #<Enumerator: 0...8:step(2)>

we can see what is enumerated:

enum.to_a                     #=> [0, 2, 4, 6]

The string

s #=> "0123456789abcdef"

is as follows after each call to the block:

i = 0: "ef23456789abcd01" 
i = 2: "efcd456789ab2301" 
i = 4: "efcdab6789452301" 
i = 6: "efcdab8967452301" 



回答5:


"0123456789abcdef".split('').each_slice(2).to_a.reverse.flatten.join('')

=> "efcdab8967452301"


来源:https://stackoverflow.com/questions/22943103/reverse-a-string-each-two-characters-with-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!