Text Substring
First five chars, last three, or a range in the middle
- substring
TextSubstring is Python string slicing with a dropdown on top. One string in, one string out, cut where you tell it to. In a pack full of text tools it's about as basic as it gets - but "basic" is the point, because when a filename or an ID has a fixed format, a reliable substring node beats fiddling with regex every single time.
The clip_mode dropdown decides how you express the cut, and there are four ways. start_len (the default) takes a start position and a length - start: 3, length: 5 on "HelloWorld" gives you "loWor". start_end takes a start and an end and slices between them, Python-style half-open (start included, end excluded). first_n and last_n are the two you'll use constantly for trimming: first_n with length: 5 gives the first five characters, last_n with length: 3 gives the last three. That last one is the killer feature - grabbing the file extension or the last few digits of an ID without counting anything.
Two details matter. First, negative indices work - start: -3 means "three from the end," which is the natural way to grab a suffix. Second, the semantics of length: 0: in start_len and the "take N" modes, a zero length means "to the end" rather than "zero characters." So first_n with length: 0 returns the whole string, not nothing. That's a deliberate convenience, and it's also the one thing that might surprise you if you came in expecting strict slicing semantics. In start_end mode, an end of 0 similarly means "to the end" - so you can't slice to "the empty string at position 0" that way. Minor, but it explains some otherwise confusing outputs.
The inputs are all optional except text, so the node is easy to wire: connect a string, pick a mode, set the numbers that matter for that mode (length for the take-N modes, start+end for range modes). Output is a single STRING called substring, ready to feed TextCase, TextJudge, or a filename builder.
Install is the pack's usual lightweight story:
cd ComfyUI/custom_nodes
git clone https://github.com/wwsmiao/comfyui-wwdm-aicd.git
Restart ComfyUI, find it under wwdm-aicd. Stdlib only, no requirements.txt in the repo regardless of what the README's install section says - the clone is everything. README and source comments are in Chinese; widget labels are English.
Where it fits: trimming a fixed-width field out of OCR output, pulling the last four characters of a hash to make a short ID, or cutting a filename down to the first 20 characters for a save path. It won't change your life, but it's the kind of node that turns a "I guess I'll do this in regex" moment into a two-second fix. Pair it with TextLength when you're not sure how long the input is, and it becomes predictable.
Inputs (5)
| Name | Type | Default | Description |
|---|---|---|---|
| text | STRING | — | |
| startopt | INT | 0 | — |
| lengthopt | INT | 0 | — |
| clip_modeopt | COMBO | start_len | 4 options: start_len, start_end, first_n, last_n |
| endopt | INT | 0 | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| substring | STRING | — |