如何将字符串切成对
How do Slice a string into pairs
对于我正在做的项目,我需要根据输入进行配对,但我不知道如何做,我需要一些帮助。
怎么样:
2209222717080109
我希望它变成这样:
["22","09","22","27","17","08","01","09"]
input = "2209222717080109"
input.chars.each_slice(2).map(&:join)
["22", "09", "22", "27", "17", "08", "01", "09"]
Convert it to a string, convert that into an array of characters, then take each consecutive slice of two characters, and join each of those slices一起:
2209222717080109.to_s.chars.each_slice(2).map(&:join)
#=> ["22", "09", "22", "27", "17", "08", "01", "09"]
"2209222717080109".scan /../
#=> ["22", "09", "22", "27", "17", "08", "01", "09"]
对于我正在做的项目,我需要根据输入进行配对,但我不知道如何做,我需要一些帮助。
怎么样:
2209222717080109
我希望它变成这样:
["22","09","22","27","17","08","01","09"]
input = "2209222717080109"
input.chars.each_slice(2).map(&:join)
["22", "09", "22", "27", "17", "08", "01", "09"]
Convert it to a string, convert that into an array of characters, then take each consecutive slice of two characters, and join each of those slices一起:
2209222717080109.to_s.chars.each_slice(2).map(&:join)
#=> ["22", "09", "22", "27", "17", "08", "01", "09"]
"2209222717080109".scan /../
#=> ["22", "09", "22", "27", "17", "08", "01", "09"]