CMS Development

randylough
Member

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

SOLVE

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?

0 Upvotes
2 Accepted solutions
piersg
Solution
Key Advisor

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

SOLVE

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, '') %}

View solution in original post

Kevin-C
Solution
Recognized Expert | Partner
Recognized Expert | Partner

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

SOLVE

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 Cornett - Sr. Solutions Architect @ BridgeRev

View solution in original post

4 Replies 4
piersg
Solution
Key Advisor

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

SOLVE

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, '') %}
Kevin-C
Solution
Recognized Expert | Partner
Recognized Expert | Partner

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

SOLVE

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 Cornett - Sr. Solutions Architect @ BridgeRev
piersg
Key Advisor

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

SOLVE

@Kevin-C Yours is much shorter and cleaner haha

0 Upvotes
benvanlooy
Top Contributor | Platinum Partner
Top Contributor | Platinum Partner

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

SOLVE

Don't forget, this won't work with every word obviously 🙂

 

From the top of my head:

0 Upvotes