Nodes/ComfyUI/Substring
ComfyUI Node Runs on cloud

Substring

Carve a slice out of any string

By Comfy-Org·Created 4 years ago·Updated 36 minutes ago· 129,963
Substring
    • STRING
    string
    start
    end

    Substring (StringSubstring) cuts a slice out of a string: give it a start position and an end position, and it returns the characters in between. It's the surgical version of text extraction - no patterns, no searching, just "the text between position X and position Y." When you know exactly where the useful part sits, this is faster and more predictable than any regex.

    Where it earns its place: trimming fixed-format text. A filename where the seed always starts at character 7. A timestamp in a fixed column of an API response. A prompt where the first N characters are a known prefix you want to strip. LLM outputs that wrap your content in a predictable envelope - "Here is your prompt: {content}" - and you want just the part after the marker. All of these are positional, and this node is the positional tool. For variable position, you'd reach for the RegexExtract node instead; this one is for text with a known shape.

    How it works

    The mechanism is Python string slicing: string[start:end]. Three inputs, one output:

    • string - the text to slice.
    • start - where the slice begins (the first character index).
    • end - where it stops.

    And here's the thing that trips up every newcomer: end is exclusive. start=0, end=5 gives you characters 0 through 4 - five characters, not six. Off-by-one is the classic Substring failure. Two more Python slicing behaviors carry over and are worth knowing:

    • Negative indices count from the end. start=-4 starts four characters before the end; end=-1 stops one character before the end. So start=-4, end=-1 grabs the last three characters - the tidy way to pull the extension off a filename.
    • Out-of-range values just clamp. Asking for start=50 on a 10-character string returns empty; asking for end=100 returns everything from start to the actual end. No errors, which keeps the graph alive but silently shortens output.

    Output: a single STRING with the slice.

    Common issues

    The off-by-one on end is the whole story of this node - double-check that you're not cutting one character short or leaving a stray one on. Second: the inputs are required integers, so if your start/end come from another node's output, make sure it's actually producing numbers (a string "5" won't wire into an INT socket). Third: remember it slices characters, not words - splitting mid-word is fine for the computer and wrong for whatever reads it next, so pair it with a whitespace-aware step if you're trying to cut at a boundary. For the common "everything from position N onward" case, set end to a big number (it clamps) - or use negative indices when you know the tail length. Small node, exact behavior, worth the two minutes to internalize.

    Categorytext

    Inputs (3)

    NameTypeDefaultDescription
    stringSTRING
    startINT
    endINT

    Outputs (1)

    NameTypeDescription
    STRINGSTRING