Splitter.SplitTextByLengths
SplitterReturns a splitter function that divides text into substrings of specified lengths.
Syntax
Splitter.SplitTextByLengths(lengths as list, optional startAtEnd as nullable logical) as functionParameters
| Name | Type | Required | Description |
|---|---|---|---|
lengths | list | Yes | A list of numbers specifying the character length of each resulting segment. |
startAtEnd | logical | No | When true, splits from the end of the string rather than the start. Defaults to false. |
Return Value
function — A function that splits text into substrings according to the specified list of lengths.
Remarks
Splitter.SplitTextByLengths returns a splitter function that, when applied to a text value, extracts consecutive substrings of the lengths specified by the lengths list. Each element of lengths defines how many characters to take for that segment.
If the total of all lengths is less than the full string length, any remaining text is returned as an additional final element. If a length extends beyond the remaining characters, only the available characters are returned for that segment.
When startAtEnd is true, lengths are applied from right to left.
This is useful for parsing fixed-width file formats where each field has a predetermined character count.
Examples
Example 1: Split into segments of 3, 4, and 3 characters
Splitter.SplitTextByLengths({3, 4, 3})("PowerQuery")Result | |
|---|---|
| 1 | Pow,erQu,ery |
Example 2: Parse a fixed-width record
let
record = "ALICE030F",
splitter = Splitter.SplitTextByLengths({5, 2, 1}),
parts = splitter(record)
in
partsThe final output — a list of the three fixed-width segments extracted from the input string.
Result | |
|---|---|
| 1 | ALICE,03,0F |
Example 3: Split from the end
Splitter.SplitTextByLengths({3, 3}, true)("ABCDEF")Result | |
|---|---|
| 1 | ABC,DEF |
Example 4: Use with Table.SplitColumn
Table.SplitColumn(
#table({"Code"}, {{"US1234"}}),
"Code",
Splitter.SplitTextByLengths({2, 4}),
{"Country", "Number"}
)Country | Number | |
|---|---|---|
| 1 | US | 1234 |