Home > database >  Divide a string into binary indexes [closed]
Divide a string into binary indexes [closed]

Time:10-05

a = '123712638126378123681273'

i have

=> a = ['12','37', ...... ]

I tried to use split but I do not know how to order it

help me

CodePudding user response:

I would do:

a.scan(/\d{2}/)
#=> ["12", "37", "12", "63", "81", "26", "37", "81", "23", "68", "12", "73"]

CodePudding user response:

Input

a = '123712638126378123681273'

Code

p a.gsub(/.{2}/).to_a

Or

p a.chars.each_slice(2).map(&:join)

Output

["12", "37", "12", "63", "81", "26", "37", "81", "23", "68", "12", "73"]
  • Related