Truncate String
Cut a String Down to Size Without Chopping a Word in Half
- STRING
You've built a filename out of a prompt fragment, a seed, and a timestamp, and now it's 200 characters long and your filesystem is about to have opinions about that. Truncate String is the node that reins it back in - and it does it more carefully than a blunt character slice would.
How it works. It wraps Python's textwrap.shorten(), which means it truncates on word boundaries, not raw character position. Feed it a string longer than your limit and it cuts at the nearest whole word rather than leaving you with a filename that stops mid-token. If the input already fits inside the length you gave it, it passes through completely unchanged - no trail string gets appended just because the node ran. The trail only shows up when truncation actually happens.
Inputs/outputs. string (required STRING) is the text to shorten. length (INT, default 48, minimum 0) is the target character width. trail (STRING, default "-") is what gets appended when the string had to be cut - so "Hello world example" at length=10 with the default trail comes back as something like "Hello-", not a hard 10-character slice. Output is a single STRING.
One honest edge case worth knowing about before you hit it and assume something's broken: set length very small - 0, or close to it - and the trail string itself can end up longer than the width you asked for, so the output actually exceeds the length you requested. That's textwrap.shorten()'s own behavior, not a bug specific to this node, but it'll surprise you if you're relying on this for a hard byte-exact filesystem limit rather than a "keep it roughly sane" pass.
Where this sits in the wider workflow: right before the finish line. Pull a checkpoint name or seed with Get Widget Value String, stamp a timestamp with Format Datetime String, glue the pieces together with Concatenate Strings - and Truncate String is the safety valve before that assembled string goes into Save Image as WEBP's filename_prefix, keeping an otherwise-unbounded string from turning into a filename your OS refuses to write.
Installing it. ComfyUI Manager: search "comfyui-yanc," install, restart. Or manually:
cd ComfyUI/custom_nodes
git clone https://github.com/ka-puna/comfyui-yanc
then restart ComfyUI. Standard library only - textwrap ships with Python itself - so there's nothing extra to install, and nothing to download.
Troubleshooting. Because truncation is word-aware, the actual output length isn't always exactly what you set in length - it can land a handful of characters short, since the node won't chop a word in half just to hit an exact count. If you need a genuinely hard character cap (a strict filesystem byte limit, say), this node's word-boundary behavior works against you, not for you - you'd want a plain slice done elsewhere in that case, not this node. Also remember the trail counts against your budget, not on top of it: length=10 with trail="..." leaves you with roughly 7 characters of actual content plus the three-character trail, which catches people who set a small length and a longish trail and end up with almost nothing but the trail itself.
Inputs (3)
| Name | Type | Default | Description |
|---|---|---|---|
| string | STRING | — | |
| length | INT | 48 | — |
| trail | STRING | - | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| STRING | STRING | — |