ComfyUI Node

String Mid

Cut any middle chunk out of a string, position and length included

By TakkunRed·Created 5 months ago·Updated 15 days ago· 1
String Mid
    • result
    ◄text►
    ◄start_pos1►
    ◄num_chars1►

    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=1 means 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_pos is 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 from text[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=0 returns 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.

    CategoryString Function

    Inputs (3)

    NameTypeDefaultDescription
    textSTRING—
    start_posINT11–99999開始位置 (1始まり)
    num_charsINT10–99999—

    Outputs (1)

    NameTypeDescription
    resultSTRING—