Page 1 of 1

Function Request: String.Truncate(length)

Posted: 29 Oct 2019, 20:11
by jbbarnes77
A really helpful addition to the available functions would be one to truncate a string at a given length.

I have found that some episode titles returned by TheTVDB are unmanageable. If you want some insane examples, look at the titles of Rocky & Bullwinkle Show episodes. (The writers obviously had a lot of fun naming them.)

This causes problems in two cases I've run into:

1) Path + filename length > 256. Once renamed, these files can't be copied elsewhere because the destination path name exceeds the maximum Windows length of 256 characters.

2) Encrypted volume constraints. One of the shares on my NAS is encrypted and that limits the maximum length of any filename to 143 characters.

Thanks.

Re: Function Request: String.Truncate(length)

Posted: 29 Oct 2019, 22:13
by rednoah
e.g.

Code: Select all

{t.take(80)}
https://stackoverflow.com/a/15713996/1514467


:idea: By default, {t} is truncated at ~150 characters.

Re: Function Request: String.Truncate(length)

Posted: 27 Dec 2025, 07:07
by iansebryk
is there a way to truncate to the nearest word after that length? for example: if the showname would normally resolve to 'Special Intake Machinator 9000' and i set {n.take(20)} it will resolve to 'Special Intake Machi'. it would be cool if it could use something like {n.take(w,3)} and resolve to 'Special Intake Machinator' instead, making it easier to recognize series titles. and, yeah, this is more applicable when sorting anime. most often i don't want the exhaustive title, just the first 'n' words of it.

Re: Function Request: String.Truncate(length)

Posted: 27 Dec 2025, 07:33
by rednoah
iansebryk wrote: 27 Dec 2025, 07:07 most often i don't want the exhaustive title, just the first 'n' words of it.
Try this:

Format: Select all

{ n.tokenize(' ').take(3).join(' ') }

:arrow: Alternatively, you can truncate to the nearest word-boundary like so:

Format: Select all

{ n.truncate(20, /\W/) }