Sunday, March 25, 2012

Inserting a character after "n"th character in a string using Ruby

I had a situation where I needed to insert a character after every 2nd character in a string(I was converting a large number of strings into LUN names)

irb(main):001:0> s1="1q2w3e4r5t6y7u"
=> "1q2w3e4r5t6y7u"
irb(main):003:0> s1.scan(/.{1,2}/).join(":")
=> "1q:2w:3e:4r:5t:6y:7u"

Same applies

No comments:

Post a Comment