tags:

views:

313

answers:

2

I have a string (e.g. "AABBCCDDEEFF") and want to split this into an array with each element containing two characters - ["AA", "BB", "CC", "DD", "EE", "FF"].

+11  A: 

Try the String object's scan method:

>> foo = "AABBCCDDEEFF"
=> "AABBCCDDEEFF"
>> foo.scan(/../)
=> ["AA", "BB", "CC", "DD", "EE", "FF"]
Chris Bunch
+10  A: 

Depending on your needs, this may work better:

>  foo = "AAABBCDEEFF"
=> "AAABBCDEEFF"
> foo.scan(/.{1,2}/)
=> ["AA", "AB", "BC", "DE", "EF", "F"]

Not sure what your input looks like. The above answer will drop any characters that do not have a pair, this one will work on odd length strings.

mletterle