ComfyUI Node

String Trim

Strip the whitespace and stray characters your LLM left behind

By TakkunRed·Created 5 months ago·Updated 15 days ago· 1
String Trim
    • result
    ◄text►
    ◄modeboth►
    ◄chars►

    String Trim strips leading and trailing characters off a string - whitespace by default, or any characters you list. It's the cleanup node in the TakkunRed ComfyUI-String-Function pack, and "cleanup" is exactly what LLM-driven workflows never have enough of. Models love trailing newlines, leading spaces, stray quote marks and curly braces that survived extraction. One Trim on the way out of an LLM node or file read removes a whole class of "why is my prompt garbage" bugs.

    The underrated part of this node is that you're not stuck with whitespace. You can give it an explicit chars set and it'll peel those off both ends - think stripping " and ' and " from around a value, or removing a trailing comma or semicolon that a generator sprinkled in.

    How it works

    Three modes, one list of characters:

    • both (default) - strip from both ends.
    • left - strip from the start only.
    • right - strip from the end only.

    Leave chars empty and it removes whitespace (spaces, tabs, newlines, the works). Put something in chars and it strips only those characters - every one you list, in any order, from whichever end the mode points at. Under the hood it's Python's str.strip / lstrip / rstrip, so behavior is exactly what those standard methods do: if chars="ab", then "abxxba" → "xx", because it keeps peeling a and b from both edges.

    One honest note from the source: this pack's left/right modes trim only the outside edge they name - there's no "trim inside the string" and no strip("a")-means-one-thing-confusion. The defaults are the sane 90% case.

    Inputs and outputs

    Two required inputs: text and mode (the three-way dropdown). One optional: chars (leave blank for whitespace). One output: result (STRING).

    Wire it between a String Extract Prompt (or any LLM output) and your encoder, and it quietly absorbs newlines and padding that would otherwise ride into conditioning.

    Installing it

    Same pack, same zero-dependency story:

    cd ComfyUI/custom_nodes
    git clone https://github.com/TakkunRed/ComfyUI-String-Function
    

    Or search "ComfyUI-String-Function" in ComfyUI Manager, restart, and it's under String Function.

    Where people get burned

    • Whitespace trimming is all-or-nothing per side. It won't collapse multiple internal spaces - "a b" stays "a b". If you need internal collapse, that's String Replace's job (" " → " ").
    • chars is a set of characters, not a substring. chars="ab" doesn't strip the literal text "ab" - it strips any a or b. For removing a specific substring from the edges, use String Replace.
    • Trim happens after extraction, not instead of it. If your extractor's success output is False, Trim can't rescue a genuinely empty result - pair it with String Default for the full safety net.
    • The whitespace default includes newlines, so a multi-line paste gets its outer blank lines peeled while inner line breaks survive - usually exactly what you want, occasionally a surprise.

    Clean text in, clean text out. It's the quietest node in the pack and one of the ones you'll use most.

    CategoryString Function

    Inputs (3)

    NameTypeDefaultDescription
    textSTRING—
    modeCOMBObothboth=前後, left=左のみ, right=右のみ
    charsoptSTRING除去する文字を列挙。空白のままにすると空白文字を除去

    Outputs (1)

    NameTypeDescription
    resultSTRING—