Nodes/ComfyUI/Trim Text
ComfyUI Node Runs on cloud

Trim Text

Kill the invisible characters before they wreck your prompt

By Comfy-Org·Created 4 years ago·Updated 37 minutes ago· 129,963
Trim Text
    • STRING
    string
    mode

    Trim Text (StringTrim) removes whitespace from the ends of a string - the spaces, tabs and newlines you can't see but absolutely are there. It sounds too boring to be worth a paragraph, until a text node somewhere upstream hands you "a red barn\n" and you watch that newline land in a filename or a prompt. Then you're glad it exists.

    In a ComfyUI graph, text isn't only the stuff you type. It flows out of JSON extraction, concatenation, regex, dataset rows, filename builders - and programmatic text almost always carries stray whitespace. Trim Text is the cheap cleanup step you drop between "whatever produced this string" and "whatever consumes it."

    How it works

    Under the hood it's one Python string method, which one depends on the mode you pick:

    • Both - strips whitespace from both ends (Python's str.strip()). This is what you want 95% of the time.
    • Left - strips only the beginning (lstrip()).
    • Right - strips only the end (rstrip()).

    It handles spaces, tabs, newlines - the whole whitespace family. It will not touch internal spacing (multiple spaces between words stay put) and it won't strip punctuation or anything else. If you need to collapse repeated internal spaces, that's a job for a regex replace, not this node.

    The inputs that matter

    Only two, and both are obvious:

    • string - the text to process. Multiline enabled, so you can paste a block in or wire another node's output into it.
    • mode - Both / Left / Right, as above.

    Both is the safe default. Left and Right exist for the rare case where you deliberately want to keep one side's whitespace - say you're trimming just the indent off a pasted block, or you want to preserve a trailing space for a later concatenation.

    The single STRING output feeds anything that takes text: a CLIPTextEncode prompt, a StringConcatenate, a filename chain, a dataset row. Since it's pure string math it runs instantly - no model, no GPU, no API key, nothing to load.

    Common gotchas

    • The frontend already trims typed text. If you type a prompt into a widget yourself, it's usually clean. Trim Text earns its keep when text is generated by other nodes, so don't be surprised if you see no change on a hand-typed string.
    • It's ends-only. Internal whitespace, double spaces, weird unicode spaces - those survive. If "clean up all whitespace" is the actual job, look elsewhere.
    • Don't confuse it with the old Strip Whitespace node. Trim Text is the current, non-deprecated version - it's one of the basic string-operation nodes ComfyUI added in mid-2025. Strip Whitespace does the same thing but only in "both ends" form, and it's marked deprecated. Wire new graphs here.

    It ships with ComfyUI core. Nothing to install, no Manager, no git clone - it's just there.

    Categorytext

    Inputs (2)

    NameTypeDefaultDescription
    stringSTRING
    modeCOMBO3 options: Both, Left, Right

    Outputs (1)

    NameTypeDescription
    STRINGSTRING