If your string contains only ASCII characters, and your byte_size string byte_size multiple of 3, there is a really elegant solution using the lesser-known Elixir function: binary recognition methods:
iex(1)> string = "UGGUGUUAUUAAUGGUUU" "UGGUGUUAUUAAUGGUUU" iex(2)> for <<x::binary-3 <- string>>, do: x ["UGG", "UGU", "UAU", "UAA", "UGG", "UUU"]
This splits the string into pieces of 3 bytes. This will be much faster than splitting on code points or graphemes, but will not work correctly if your string contains non-ASCII characters. (In that case, I would go with @michalmuskala's answer.)
Edit: Patrick Oscity's answer reminded me that this could also work for code pages:
iex(1)> string = "αβγδεζηθικλμνξοπρςστυφχψ" "αβγδεζηθικλμνξοπρςστυφχψ" iex(2)> for <<a::utf8, b::utf8, c::utf8 <- string>>, do: <<a::utf8, b::utf8, c::utf8>> ["αβγ", "δεζ", "ηθι", "κλμ", "νξο", "πρς", "στυ", "φχψ"]
source share