ComfyUI Node

String Find

Where in this string is that text? Ask, get a number

By TakkunRed·Created 5 months ago·Updated 15 days ago· 1
String Find
    • position
    ◄text►
    ◄search►
    ◄start_pos1►
    ◄occurrence1►

    String Find answers the question "where does this substring start?" with a plain integer. You give it some text and a search string; it returns the 1-based character position of the match - or 0 if there's no match at all. It's the positioning tool in the TakkunRed ComfyUI-String-Function pack, and it earns its keep whenever you need to decide where in a string something lives before you cut it.

    You'll most often reach for it as the measuring stick in front of a String Mid or String Split: "the filename always starts 5 chars after 'ID: ', so find that marker and slice from there." That's the workflow you can't build with plain search-within-a-node - you need the offset as a number you can wire into something else.

    How it works

    Under the hood it's a loop over Python's str.find(), with two knobs that make it more than a bare search:

    • start_pos (default 1) - where to begin looking. It's 1-based like everything else in this pack, so a value of 1 means "start at the first character."
    • occurrence (default 1) - which match to return. occurrence=2 returns the position of the second occurrence of the search string, not the first. Combined with start_pos you can skip the first N hits and take the next.

    The convention matters: 0 means "not found," not "position zero." Positions are 1-based, so a real match always returns 1 or higher. That's a deliberate, pack-wide choice - String Mid uses the same 1-based indexing, so the two nodes agree with each other and with what Excel users expect.

    Inputs and outputs

    Two required inputs - text and search - plus the two optional knobs above. A single output: position (INT). If the search string is empty or never found, you get 0.

    Installing it

    Same pack, same nothing-to-download story:

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

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

    Where people get burned

    • Off-by-one everywhere. The output is 1-based and start_pos is 1-based, but if you feed the result straight into a Python-style slice or a String Mid, remember String Mid's start_pos is also 1-based - they line up, which is the point. The confusion starts when you assume one of them is 0-based.
    • 0 is ambiguous between "not found" and "empty string match." If you search for an empty string, behavior falls through to the not-found path and returns 0. Don't use it to probe for empty strings - that's String Default's job.
    • It finds the first match from the start position, in order. There's no regex and no reverse search here - that's String Regex Match's territory if you need patterns.

    One small node, one number, and it slots into the slicing trio (Left / Mid / Right) to make parse-the-prompt graphs you can actually read.

    CategoryString Function

    Inputs (4)

    NameTypeDefaultDescription
    textSTRING—
    searchSTRING—
    start_posoptINT11–99999検索を開始する位置 (1始まり)
    occurrenceoptINT11–999何番目の出現を検索するか

    Outputs (1)

    NameTypeDescription
    positionINT—