Function Request: String.Truncate(length)

All your suggestions, requests and ideas for future development
Post Reply
jbbarnes77
Posts: 6
Joined: 26 Oct 2019, 19:56

Function Request: String.Truncate(length)

Post 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.
User avatar
rednoah
The Source
Posts: 24342
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Function Request: String.Truncate(length)

Post 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.
:idea: Please read the FAQ and How to Request Help.
iansebryk
Posts: 15
Joined: 28 Jul 2020, 11:51

Re: Function Request: String.Truncate(length)

Post 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.
User avatar
rednoah
The Source
Posts: 24342
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Function Request: String.Truncate(length)

Post 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/) }
:idea: Please read the FAQ and How to Request Help.
Post Reply