String Count
Count how many times a word shows up in your text
- count
String Count is the tally counter: "how many times does this substring appear in that string?" It returns a plain integer, and it's surprisingly handy for sanity-checking text before you let it loose on a model. Whether a prompt has the right number of parts, or a string is the shape you think it is - Count gives you a number you can branch on.
What it is
It runs Python's s.count(value), which counts non-overlapping occurrences. So counting "aa" in "aaaa" gives you 2, not 3 - Python scans left to right and doesn't let matches overlap. If value isn't found, you get 0, not an error. If value is empty, Python's str.count("") returns one more than the string length, which is a weird edge case you'll almost never need.
Practical uses: count commas in a CSV-ish prompt to estimate how many tags are in it, count how many times a style token like "masterpiece" got duplicated in an auto-generated prompt, or verify a fixed-format string has the expected number of a character. Pair the integer with a comparison node or the pack's conditional booleans and you can route on it.
The inputs and outputs
s(STRING) - the text to search.value(STRING) - the substring to count.count(INT) - how many non-overlapping timesvalueappears ins.
Two in, one out. No settings, no modes.
How to install it
Part of the string-util pack, a dependency-free repo like every node in it:
cd ComfyUI/custom_nodes
git clone https://github.com/kale4eat/ComfyUI-string-util
or search "ComfyUI-string-util" in ComfyUI Manager and restart. It lives under the string-util category.
Common issues
The two gotchas are Python quirks, so they're worth knowing cold. First, counts are case-sensitive: "masterpiece" is not counted by a search for "Masterpiece". Normalize both sides with String Upper or String Lower first if you want case-insensitive counting. Second, the count is non-overlapping, which only bites for repeated patterns like "aaa". And if you're counting to estimate token length, remember a character count is not a token count - this measures characters, so treat it as a rough shape check, not a token budget.
Inputs (2)
| Name | Type | Default | Description |
|---|---|---|---|
| s | STRING | — | |
| value | STRING | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| count | INT | — |