ComfyUI Node

String Replace

Find-and-replace in your graph, with a count of what it changed

By TakkunRed·Created 5 months ago·Updated 15 days ago· 1
String Replace
    • result
    • replace_count
    ◄text►
    ◄old►
    ◄new►
    ◄count-1►
    ◄case_sensitivetrue►

    String Replace is find-and-replace for a single text field: give it text, an old string, a new string, and it swaps every occurrence (or a limited number) and hands back the result - plus a count of how many replacements actually happened. It's from the TakkunRed ComfyUI-String-Function pack, and it's the go-to when a prompt, caption or LLM output contains a thing you want gone or standardised.

    The prompt-specific uses are the satisfying ones. Quality tags that mean nothing on a 2026 LLM-encoded model (masterpiece, best quality, 8k - the KB's notes on the genre call them dead weight there), a character name you want renamed across a generated description, a stray (word:1.4) weight syntax you want stripped out before it becomes literal punctuation. That last one is a genuinely useful trick: replace (something:1.4) patterns with clean text before an LLM encoder sees them, since prompt weighting is dead on those architectures and the syntax rides through as literal tokens otherwise.

    How it works

    Two code paths, one count. With case_sensitive=True (default) it's a plain str.replace with an optional count limit. With case_sensitive=False it splits on the pattern with re.IGNORECASE and rejoins, which is how it does case-insensitive replacement without regex syntax leaking in.

    The count input is the useful knob: default -1 means "replace everything." Set it to 1 to replace only the first occurrence - that's how you target the first offending tag in a list while leaving later legit uses alone. And replace_count (the second output) tells you whether anything was actually replaced - a 0 means your old string didn't appear at all, which is a debugging gift when you're sure it should have.

    Inputs and outputs

    Required: text, old, new. Optional: count (default -1, replace all), case_sensitive (default True). Outputs:

    • result (STRING) - the text after replacement.
    • replace_count (INT) - how many occurrences were swapped.

    Installing it

    Same pack, same no-dependencies install:

    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

    • Empty old means no-op. The source guards this explicitly - an empty search string returns the text unchanged with replace_count=0. So "clear this field" is String Default's job, not a Replace with an empty old.
    • Case-insensitive mode still replaces the whole matched text with your literal new. The original casing isn't preserved - "Photo" in, replace photo→image case-insensitively, and you get image, not a case-aware hybrid.
    • count counts occurrences, not characters. count=1 with old=" " removes one space, not the first word.
    • It's literal text, not regex. If you need to match \d+ or a pattern, that's String Regex Match (which can extract but not replace) or a replace via a code path that supports patterns - this node deliberately does not.

    For dead-tag removal and consistent naming across generated text, it's the node you'll wire up once and never think about again.

    CategoryString Function

    Inputs (5)

    NameTypeDefaultDescription
    textSTRING—
    oldSTRING—
    newSTRING—
    countoptINT-1-1–99999置換する最大回数。-1 は全て置換
    case_sensitiveoptBOOLEANtrueFalse にすると大文字小文字を無視して置換

    Outputs (2)

    NameTypeDescription
    resultSTRING—
    replace_countINT—