String Mid
Cut any middle chunk out of a string, position and length included
- result
String Mid extracts a chunk from the middle of a string: starting at a given position, take a given number of characters. It's the Excel MID equivalent in the TakkunRed ComfyUI-String-Function pack, and it's the one slicing node you can't fake with the other two - Left and Right only get you the edges, Mid gets you the inside.
The canonical use is parsing a fixed-width field: the model name in a filename always starts at character 9 and is 12 characters long; the scene descriptor in a generated line always runs from position 15. Where text has a rigid layout, Mid is the surgical instrument - feed it a position and a length and the middle comes out clean.
How it works
Two numbers, one rule:
start_pos(default 1) - where to start, 1-based like every other node in this pack.start_pos=1means the first character.num_chars(default 1) - how many characters to take after that.
Under the hood it's a slice from start_pos - 1 to start_pos - 1 + num_chars, clamped so out-of-range values just return what's available rather than erroring. That 1-based indexing is the detail to hold onto: it matches String Find's output, so "find where the marker is, then Mid from there" composes without mental off-by-one surgery.
Inputs and outputs
Three required inputs: text, start_pos, num_chars. One output: result (STRING).
It pairs naturally with String Find, which hands you the start_pos as an integer from a live search - that's the graph pattern for "slice everything after the label" that beats hardcoding positions.
Installing it
Same pack, same no-dependencies install:
cd ComfyUI/custom_nodes
git clone https://github.com/TakkunRed/ComfyUI-String-Function
Or search "ComfyUI-String-Function" in ComfyUI Manager, restart, and look under String Function.
Where people get burned
- The off-by-one is the whole game.
start_posis 1-based; a lot of people assume it's 0-based like Python's own slicing and end up off by one character. If you're coming fromtext[start:]habits, subtract one. - It's positional, not structural. Mid can't find "the part between the quotes" - that's String Split (delimiter) or String Regex Match (pattern) territory. Mid only knows positions.
num_chars=0returns an empty string, which is a fine way to build a "disable this chunk" switch by driving the count from a widget.
Fixed layouts don't come up in every workflow, but when they do - and in this pack's world of parsed filenames and generated text they do - Mid is the one you reach for.
Inputs (3)
| Name | Type | Default | Description |
|---|---|---|---|
| text | STRING | — | |
| start_pos | INT | 11–99999 | 開始位置 (1始まり) |
| num_chars | INT | 10–99999 | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| result | STRING | — |