PassiveSupport.String.length_split
length_split
, go back to PassiveSupport.String module for more information.
length_split(string, lengths, opts \\ [first_split: false])
Specs
length_split(String.t(), integer() | [integer()], [{:first_split, boolean()}]) :: String.t() | [String.t()] | [[String.t()]]
Splits a string by a given length or lengths.
When one length is given, splits the string into a list of substrings of that length.
When a list of lengths is given, returns a list of lists of substrings of the given lengths.
If the string does not fit within the given length(s), the final substring will be the length of the remainder of the string.
To retrieve only the first length
or lengths
of the string,
pass first_split: true
. Note that in the case of a single length
,
this is equivalent to calling String.slice(string, 0..length)
, or
binary_part(string, 0, length)
. This is useful when, while supplying
multiple lengths, only the first lengths
of the given string are important
to the program, or when the sum of lengths
is equal to the length
of the original string.
Examples
iex> length_split("hello world!", 3)
["hel", "lo ", "wor", "ld!"]
iex> length_split("hello world!", 5)
["hello", " worl", "d!"]
iex> length_split("hello world!", 5, first_split: true)
"hello"
iex> length_split("Life, the universe, and everything... is pattern-matchable", [10, 9, 7])
[
["Life, the ", "universe,", " and ev"],
["erything..", ". is patt", "ern-mat"],
["chable"]
]
iex> length_split("Life, the universe, and everything... is pattern-matchable", [10, 9, 7], first_split: true)
["Life, the ", "universe,", " and ev"]