ComfyUI Node

Text Trimmer

The whitespace janitor with the right modes

By DebugPadawan·Created about a year ago·Updated 5 months ago· 2
Text Trimmer
    • trimmed_text
    text hello world
    mode

    Text Trimmer is a six-mode whitespace cleaner, and it's the version of this idea you actually want. Its sibling in the pack, Text Trim, covers the basics; this one adds the modes that make it genuinely useful for messy real-world text.

    The job, concretely: clean text before it goes somewhere that cares about exact characters. Prompts pasted from a website, tag lists scraped from CivitAI, output copied out of an LLM chat window - all of it arrives carrying invisible baggage. Trailing spaces, doubled spaces, stray newlines. Text Trimmer is the scrub step you drop in front of a wildcard processor, an LLM prompt node, or a filename builder so the junk doesn't survive into the actual generation or file name.

    How it works - the modes

    • both / start / end - plain strip, lstrip, rstrip. The basics.
    • collapse_spaces - collapses any run of whitespace anywhere in the string to a single space. " Hello World ""Hello World". This is the workhorse.
    • all_whitespace - removes every whitespace character entirely. " Hello World ""HelloWorld". Rarely what you want, but perfect when you're building compact IDs or hashes.
    • remove_newlines - replaces \n and \r with a single space, so a multi-line block becomes one line without collapsing interior spacing. Great for pasting a paragraph into a prompt field.

    One input (text, multiline), one output (trimmed_text). That's the whole node.

    Why it beats Text Trim

    The two nodes genuinely overlap - Text Trim's all mode is the same thing as collapse_spaces here. But Trimmer gives you the full ladder: a true strip, a collapse, a remove-everything, and a newline-only pass. For pasted-text cleanup the newline mode and collapse mode are the difference between "good enough" and "actually clean." If both are in your graph, you're probably looking at a workflow built before the author added the newer node - swap to this one and delete the other.

    Installation

    Same pack, same routine:

    cd ComfyUI/custom_nodes
    git clone https://github.com/DebugPadawan/DebugPadawans-ComfyUI-Essentials.git
    

    or ComfyUI Manager → search "DebugPadawan's ComfyUI Essentials" → install → restart. No model downloads; this is pure string handling and needs nothing extra (the pack's numpy/torch/opencv requirements belong to its image and math nodes).

    Gotchas

    Two behaviors are worth knowing before you're surprised. First, collapse_spaces (and all_whitespace) operate on the entire string - interior spacing changes, not just the edges. A deliberately indented or multi-spaced prompt will not survive untouched. Second, remove_newlines replaces newlines with a space but leaves doubled spaces alone, so text pasted with line breaks and spacing can come out with awkward gaps - run it through collapse_spaces after if that matters.

    There's no error surface here to speak of: a string goes in, a string comes out. If the output looks wrong, it's almost always non-visible characters your eyes skipped - a tab (handled), a non-breaking space (not stripped by the plain modes), or a Unicode space from copied web text. When in doubt, check the raw character codes of the input instead of squinting at it.

    CategoryDebugPadawan/Text

    Inputs (2)

    NameTypeDefaultDescription
    textSTRING hello world
    modeCOMBO6 options: both, start, end, all_whitespace, collapse_spaces, remove_newlines

    Outputs (1)

    NameTypeDescription
    trimmed_textSTRING