Nodes/ComfyUI-Buff-Nodes/String Processor (Buff)
ComfyUI Node

String Processor (Buff)

Slice, dice, and eval your strings without leaving the graph

By BuffMcBigHuge·Created 2 years ago·Updated 20 days ago· 2
String Processor (Buff)
    • processed_string
    input_string
    operationfirst_n_chars
    n1
    delimiter
    custom_expression{s}.upper()

    String handling in ComfyUI is usually a string of disappointments: a node that uppercases, a node that replaces, a node that does one very specific thing you need exactly 2% of the time. StringProcessor bundles the common operations - take the first/last N characters, take the first/last N words - and then adds an escape hatch: a custom Python expression you type directly into the node. It's the Swiss-army string tool that covers the 98% you can't find a dedicated node for.

    Realistically, you'll use it for filename cleanup, metadata tweaks, prompt formatting, or any place a value needs reshaping before it hits a text encoder or a save node. The "last 4 characters of this filename to get the extension" class of problem is its bread and butter.

    How it works

    Pick an operation, set n, and it does what it says - first_n_chars, last_n_chars, first_n_words, last_n_words. The word operations split on a delimiter you provide (default single space; set it to , for comma-separated lists) and rejoin with the same delimiter, so you're not left gluing fragments back together.

    The interesting mode is custom_expression. You write Python using {s} as a placeholder for the input string, and the node evaluates it with s bound to the input. The tooltip ships a solid cheat-sheet: {s}.upper(), {s}.replace('old','new'), {s}[::-1] to reverse, {s}.split(',')[0] for CSV, even list comprehensions for title-casing. If the expression throws, the node returns an error string (Error in custom expression: ...) instead of crashing the graph, which is friendlier than most.

    Fair warning, stated plainly: this runs eval() on your expression. It's a controlled scope (just s), and ComfyUI custom nodes are already arbitrary Python with full user privileges, so it's not a new risk surface - but don't paste expressions from untrusted strangers into it.

    Inputs

    • input_string - what you're processing (multiline allowed).
    • operation - one of the five modes.
    • n - character/word count for the slice operations.
    • delimiter - only used by word ops.
    • custom_expression - only used in custom mode.

    Single output: processed_string.

    Install

    From BuffMcBigHuge's QoL pack - ComfyUI Manager (search "ComfyUI-Buff-Nodes") or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/BuffMcBigHuge/ComfyUI-Buff-Nodes
    

    Restart ComfyUI. No dependencies beyond stock.

    Gotchas

    The slice ops operate on the raw string, so first_n_words with a delimiter of " " treats runs of whitespace literally - double spaces can make it "skip" a word from your point of view. Empty input returns an empty string. And if a custom expression returns a non-string, it gets stringified silently, which is usually fine but occasionally surprising (a list becomes its repr). For the everyday filename-and-prompt surgery, the built-in ops cover 90% of needs and the expression field covers the other 10% with full Python. Hard to ask for more from one small node.

    Categoryutils

    Inputs (5)

    NameTypeDefaultDescription
    input_stringSTRINGThe input string to process. Can be multiple lines.
    operationCOMBOfirst_n_charsSelect the operation to perform on the input string: • first_n_chars: Extract the first N characters from the input string • last_n_chars: Extract the last N characters from the input string • first_n_words: Extract the first N words from the input string (using delimiter) • last_n_words: Extract the last N words from the input string (using delimiter) • custom_expression: Use a custom Python expression to manipulate the string
    nINT11–10000Number of characters or words to extract
    delimiteroptSTRING Character(s) used to separate words (only used for word operations)
    custom_expressionoptSTRING{s}.upper()Custom Python expression to manipulate the input string. Use {s} as a placeholder for the input string. Examples: • {s}.upper() - Convert to uppercase • {s}.lower() - Convert to lowercase • {s}.replace('old', 'new') - Replace text • {s}[::-1] - Reverse the string • {s}.split(',')[0] - Get first item of CSV • ' '.join([w.capitalize() for w in {s}.split()]) - Title case • {s}.strip() - Remove whitespace from ends • {s} + ' (modified)' - Append text

    Outputs (1)

    NameTypeDescription
    processed_stringSTRING