我有一个字符串(例如"AABBCCDDEEFF")并希望将其拆分为一个数组,每个元素包含两个字符 - ["AA","BB","CC","DD","EE","FF"] .
尝试String对象的扫描方法:
>> foo = "AABBCCDDEEFF" => "AABBCCDDEEFF" >> foo.scan(/../) => ["AA", "BB", "CC", "DD", "EE", "FF"]
根据您的需要,这可能会更好:
> foo = "AAABBCDEEFF" => "AAABBCDEEFF" > foo.scan(/.{1,2}/) => ["AA", "AB", "BC", "DE", "EF", "F"]
不确定您的输入是什么样的.上面的答案将丢弃没有一对的任何字符,这个字符将用于奇数长度的字符串.