ComfyUI Node

String Slice

Cut a substring out by character positions — carefully

By kale4eat·Created 2 years ago·Updated 2 years ago· 7
String Slice
    • string
    s
    start0
    end0

    String Slice pulls a substring out of a string using character positions - start and end. It's Python's string slicing exposed as two integers, and it's genuinely powerful for extracting fixed-position data. It also has a default-value trap that will return you an empty string if you're not careful, so read this before you wire it up.

    What it is

    It runs s[start:end] with two integer inputs, start and end. Python slicing rules apply:

    • Positions are 0-based. "hello" sliced start=1, end=3 gives "el".
    • The end position is exclusive - the character at end is not included.
    • Negative indices count from the end: start=-3 means "three from the end."

    The trap: both inputs default to 0, and s[0:0] is the empty string. So if you set start=2 and leave end at its default 0, you get "" - not "everything from position 2 onward," because a zero end means "stop at position 0," and position 0 is before position 2. To slice to the end of the string, you must set end to a large number (say 9999) - s[2:9999] correctly returns everything from position 2 to the end. That's the single most common way this node produces baffling empty output.

    The inputs and outputs

    • s (STRING) - the source text.
    • start (INT, default 0) - where the cut begins.
    • end (INT, default 0) - where the cut stops, exclusive.
    • string (STRING) - the extracted substring.

    Useful recipes: strip a fixed prefix (start=3, end=9999), drop the last character (start=0, end=-1), grab the last three characters (start=-3, end=9999).

    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's under the string-util category.

    Common issues

    The default-end-of-0 trap is the big one - set end explicitly whenever you slice. Beyond that, remember slicing is by character, not by word, so cutting a "first 10 words" needs a word-split approach (String Split on spaces, then join), not this node. And if you slice into the middle of a UTF-8 emoji or non-English character, you can split a multibyte character in half - Python counts code points here, so keep your cuts at whole-character boundaries.

    Categorystring-util

    Inputs (3)

    NameTypeDefaultDescription
    sSTRING
    startINT0
    endINT0

    Outputs (1)

    NameTypeDescription
    stringSTRING