ComfyUI Node

Substring

Truncating Text in ComfyUI Without a Python Excursion

By godspede·Created 3 years ago·Updated about a year ago· 0
Substring
    • STRING
    text
    length75

    The name is the whole pitch: Substring takes a string and a length, and hands you back the first length characters. No API, no API key, no model download, no dependencies you didn't already have. It's a 15-line Python node from the godspede/ComfyUI_Substring pack, and it exists because ComfyUI's core node set is oddly shy about basic text surgery. You can concatenate strings, but if you want to cut one down, the built-ins shrug.

    That gap is part of a bigger story. ComfyUI exposes inference as a visual graph, which is exactly what makes it so powerful and exactly why the community keeps building little utility packs like WAS Node Suite, rgthree, and Efficiency Nodes to fill the seams the graph leaves open. Substring is the micro-est end of that genre: not a workflow toolbelt, just one clean cut. It won't change your life, but if you've ever had a prompt, a filename, or a text-file readout run too long and wished for a pair of scissors in the graph, it's the pair of scissors.

    How it works

    The mechanism is literally Python string slicing. With length at 75, text[:75] comes back - the first 75 characters. That's it. Two quirks in the source are worth knowing because they'll trip you up exactly once:

    • Negative length means the tail, not "skip the last N". If you feed it -10, it returns text[-10:]... no wait, actually it returns text[length:], so -10 drops the first 10 characters and keeps everything after. Want the last 10 characters? That's text[-10:], which this node doesn't give you. The author went with "negative means drop from the front."
    • The "undefined" special case. ComfyUI widgets can hand a node the literal string "undefined" when a field is left empty in certain contexts, so the node catches it and converts it to "". If you ever wire something empty in and get an empty string out where you expected garbage, that's this working as intended.

    It's also flagged as an output node, so the result shows up in a little text widget on the graph and travels down the wire - handy when you're troubleshooting what a string actually contains.

    The inputs that matter

    • text - a STRING input with multiline enabled. Multiline matters: prompts and pasted blobs of text are usually multi-line, and a single-line field would mangle them before you even slice.
    • length - an INT, default 75. If you leave it alone, anything longer than 75 characters gets quietly truncated, so set it deliberately.
    • Output - a single STRING. Wire it straight into a CLIP Text Encode prompt input, another text node, or any string slot in your graph.

    Installing it

    The pack has zero dependencies and zero model files - pyproject.toml lists nothing but the repo itself, and the whole codebase is one file. So install is about as painless as it gets:

    cd ComfyUI/custom_nodes
    git clone https://github.com/godspede/ComfyUI_Substring
    

    Then restart ComfyUI. Or, if you'd rather, install it through ComfyUI Manager by searching "ComfyUI Substring" - the pack is on the Comfy Registry. The node shows up in the utils category under the display name Substring.

    Common issues

    Honestly, there isn't much to troubleshoot on a node this small. The realistic failure modes: the node doesn't appear (you forgot the restart), or you forget the default is 75 and wonder why your long prompt got chopped mid-word - note it cuts by characters, not words, so you may need to tune the length up. If the text field shows undefined, it's not broken, the node is just guarding against an empty widget. And if you search the community for this pack you'll find essentially nothing - it has no buzz, zero reddit footprint - which is fine. It's a utility you install once, use occasionally, and forget you own until the moment you need to trim a string and can't believe ComfyUI didn't ship this.

    Categoryutils

    Inputs (2)

    NameTypeDefaultDescription
    textSTRING
    lengthINT75

    Outputs (1)

    NameTypeDescription
    STRINGSTRING