Nodes/ComfyUI_StringOps/替换第n次出现🐠meeeyo.com
ComfyUI Node

替换第n次出现🐠meeeyo.com

Replace only the 3rd 'X' — or every one, if you set it to 0

By MeeeyoAI·Created 2 years ago·Updated about a year ago· 209
替换第n次出现🐠meeeyo.com
    • STRING
    original_text
    occurrence1
    search_str替换前字符
    replace_str替换后字符

    Plain .replace() is all-or-nothing: every X becomes Y, or none of them do. ReplaceNthOccurrence gives you the middle ground - change only the n-th time a string appears and leave the rest untouched. The Chinese display name is 替换第n次出现, literally "replace the n-th occurrence." If you've got a template where the second name is the one that's wrong, this is the surgical tool.

    It's part of ComfyUI_StringOps, and it sits alongside ReplaceMultiple (which walks a whole list of replacements) and SimpleTextReplacer (which does plain replace-all). This one is the position-targeted middle child.

    How it works

    Four inputs:

    • original_text - the multiline source.
    • search_str - what to look for. Default is the Chinese placeholder 替换前字符 ("character before replacement") - a placeholder, not a real value. Change it.
    • replace_str - what to put in its place. Default 替换后字符 ("character after replacement"). Also a placeholder.
    • occurrence - which match to hit. Default 1 (the first). Set it to 0 and the behavior flips: it replaces all occurrences, exactly like plain .replace().

    Under the hood it uses a regex substitution with a counter - each match decrements a counter and only the match that hits zero gets swapped. Because it's a proper regex, your search string is escaped, so literal punctuation like ( or . is treated as text, not regex syntax. That's friendly.

    So with original_text = "cat dog cat", search_str = "cat", replace_str = "bird", and occurrence = 2, you get cat dog bird. Set occurrence to 1 and you get bird dog cat.

    Where you'd use it

    Multi-token templates again - say a sentence that names the same subject twice, and you want the second mention to carry a descriptor. Or editing a prompt where a weighted token appears several times and only one position is misweighted. It's also the manual fallback when ReplaceMultiple's comma-separated list can't hold a value you need (its no-comma rule bites here).

    Output is a single STRING.

    The traps

    Two things to internalize. First, occurrence = 0 means replace everything - if you set it to 0 expecting "nothing happens," you'll get the opposite. Second, the "n-th" count starts at 1 and walks left to right; there's no "last occurrence" mode, so for the final match you'd have to count occurrences first (the pack has CountOccurrences for that). And remember the defaults are Chinese placeholder text - if you hit run without touching search_str, you'll wonder why nothing changed (and honestly, nothing will, because the literal placeholder probably isn't in your text).

    Installing it

    Pack standard: ComfyUI Manager → ComfyUI_StringOps, or

    cd ComfyUI/custom_nodes
    git clone https://github.com/MeeeyoAI/ComfyUI_StringOps.git
    

    Restart. Pure Python string handling - no extra dependencies, no models.

    The honest take

    Small, sharp, occasionally exactly what you need. If you do a lot of template surgery on prompts, having ReplaceNthOccurrence in the drawer beats building the same thing out of regex nodes. But for most everyday cleanup, SimpleTextReplacer is the one you'll reach for first; this is the precision instrument for when that's not enough.

    CategoryMeeeyo/String

    Inputs (4)

    NameTypeDefaultDescription
    original_textSTRING
    occurrenceINT1
    search_strSTRING替换前字符
    replace_strSTRING替换后字符

    Outputs (1)

    NameTypeDescription
    STRINGSTRING