2009-02-19 106 views

回答

37

嘗試String對象的scan方法:

>> foo = "AABBCCDDEEFF" 
=> "AABBCCDDEEFF" 
>> foo.scan(/../) 
=> ["AA", "BB", "CC", "DD", "EE", "FF"] 
24

根據您的需求,這可能會更好地工作:

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

不知道您輸入的樣子。上面的答案會刪除沒有一對的字符,這個字符將用於奇數長度的字符串。