Is it possible to trim a string by 1 character or length?

I’m looking for a way to trim a string by 1 character - ie. make something singular.

My use case:

I have a list of blog topics - “eBooks” “Templates” etc. and for each post I’m looking for a way to call the topic name and then make it singluar - eg. “Download eBook” or “Download Template”

Any ideas on how I can achieve this?

Don’t forget, this won’t work with every word obviously :slightly_smiling_face:

From the top of my head:

By the way, if you’re still looking to do this, here’s a way to cut off the last character from a word that will work with every word:

{% set topic = contents_topics [set to whatever variable or string you want] %}
{% set topicLen = topic|length %}
{% set topicLen = topicLen - 1 %}
{% set topic = topic|truncate(topicLen, True, '') %}

Hey @randylough

Expanding on the answer above you could also get the result in two ways:

1) Write it as an expression vs setting the variables:

{{ topic|truncate((topic|length - 1), True, '') }}

2) Creating a macro if you plan on doing it a lot

{% macro shortStr(str, num) %}
 {{ str|truncate((str|length - num), true, end='')}}
{% endmacro %}
{{ shortStr(topic, 1) }}

All credit to @piersg

@Kevin-C Yours is much shorter and cleaner haha