文本出现次数🐠meeeyo.com
Count how many times a string appears — as both a number and text
- INT
- STRING
CountOccurrences counts how many times a given substring shows up in a block of text, and reports the result twice - as an INT and as a STRING. Two outputs, same number, so the count can go to a math/switch port and into a text label without a conversion node in between. It's part of MeeeyoAI/ComfyUI_StringOps.
The straightforward use: count how many times a tag or keyword appears in a prompt. "Does this prompt overuse the word 'portrait'?" - now it's a number you can compare, gate on, or log.
The inputs
- input_text - the multiline text to search.
- char - the substring to count (default
查找符, the Chinese placeholder for "search char"). Despite the name, it counts any string, not just single characters -"photo"counts whole-word-ish occurrences just fine.
The hidden feature
There's a genuine gotcha and a hidden feature in the way the count works:
- If
charis the literal two characters\n(backslash-n, typed as text - not an actual newline), the node switches modes and counts non-blank lines instead of a substring. That's the intended way to get a line count out of this node. But it's a string comparison, so\nonly triggers the mode when typed exactly that way; put a real newline in the field and you're counting newline characters, which is a different thing. - Any other value is a plain substring count, and overlapping matches don't count twice: in
"aaa", counting"aa"gives 1, not 2. Standard Python behavior, but it surprises people the first time.
Where it earns its keep
Pair it with CompareInt (same pack) to turn a raw count into a bucketed label - "how many commas is too many commas" becomes a routing decision. Use it to verify a BatchReplaceStrings cleanup actually removed every instance of a token. Or feed the STRING output into a filename or log so the count is visible without any number-to-text conversion.
The INT and STRING outputs are mirror values - same count, different type - so you don't need to pick one. Wire both if the graph wants both.
Installing
Standard pack install:
cd ComfyUI/custom_nodes
git clone https://github.com/MeeeyoAI/ComfyUI_StringOps.git
or via ComfyUI Manager searching ComfyUI_StringOps, then restart. Under "Meeeyo/String". No models, no dependencies to fetch.
A small node with a clean job and one hidden mode. Remember the \n-as-line-count trick and it covers both "count this substring" and "count my lines" with a single tool.
Inputs (2)
| Name | Type | Default | Description |
|---|---|---|---|
| input_text | STRING | — | |
| char | STRING | 查找符 | — |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| INT | INT | — |
| STRING | STRING | — |