Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove trailing whitespaces from string in Jinja?

Tags:

jinja2

In my case all the strings are single-worded and the trailing whitespaces from the strings need to be removed, e.g., 'hello ''hello'; 'hello ''hello'.

One approach could be using split, i.e,

{% if ' ' in word %}
  {% set word = word.split(' ')[0] %}
{% endif %}

This works since the strings are one-worded. I am quite sure there must be a better approach.

like image 451
Ayan B. Avatar asked Nov 07 '18 12:11

Ayan B.


People also ask

What method removes only trailing whitespaces from a string?

Use the . rstrip() method to remove whitespace and characters only from the end of a string.

How do you remove leading and trailing whitespaces from a string?

We can eliminate the leading and trailing spaces of a string in Java with the help of trim(). trim() method is defined under the String class of java. lang package.

How do I remove spaces from a string?

strip()—Remove Leading and Trailing Spaces. The str. strip() method removes the leading and trailing whitespace from a string.

What are trailing whitespaces?

Trailing whitespace. Description: Used when there is whitespace between the end of a line and the newline.


2 Answers

Silly me! It is .strip(), i.e., 'hello '.strip().

like image 111
Ayan B. Avatar answered Sep 19 '22 18:09

Ayan B.


It's not .strip() ... it's | trim . Such as {{ some_variable_string_name | trim }}

like image 35
Nick Weimer Avatar answered Sep 19 '22 18:09

Nick Weimer