How to quickly remove spaces or specific characters from both ends, left or right of a string in AnQiCMS template?

Calendar 👁️ 80

In content management, we often encounter situations where there are extra spaces or unwanted specific characters at both ends of strings, which not only affects the display aesthetics of the content but may also cause unnecessary interference in data processing or search engine optimization (SEO).AnQiCMS uses a template engine syntax similar to Django, providing us with concise and efficient filters (Filters) to easily solve these problems.This article will introduce how to quickly remove spaces or specific characters from both ends, left or right of a string in AnQiCMS template.

Clean spaces or specific characters at the beginning and end of a string:trimFilter

When we want to remove all leading and trailing spaces from a string, or remove specific leading/trailing characters,trimThe filter is your powerful assistant.

Basic usage: Remove leading and trailing spaces.

If you need to remove all whitespace characters from both ends of a string (including spaces, tabs, newlines, etc.), you just need to pass the string variable through the pipe symbol|Connected totrimthe filter.

For example, assume you have a string variablearticleTitleThe value is" AnQiCMS 是一个内容管理系统 ":

{# 移除字符串两端的所有空白字符 #}
{{ articleTitle|trim }}
{# 输出结果:AnQiCMS 是一个内容管理系统 #}

Advanced usage: remove the specific characters from the beginning and end

trimThe filter also supports specifying the specific characters to be removed. You cantrimA string parameter is passed in, and each character of the parameter is considered a 'deletable character'.trimIt will check and remove these characters from both ends of the string until it encounters a mismatched character.

For example, if you want to remove from the string"***安企CMS***"from*characters:

{# 移除字符串两端的指定字符 "*" #}
{% set messyString = "***安企CMS***" %}
{{ messyString|trim:"*" }}
{# 输出结果:安企CMS #}

See an example of removing multiple different characters. If you want to remove from a string"-+-安企CMS-+- "from-/+and spaces:

{# 移除字符串两端的指定字符 "-+ " #}
{% set mixedString = "-+-安企CMS-+- " %}
{{ mixedString|trim:"-+ " }}
{# 输出结果:安企CMS #}

It is worth noting that,trimThe filter only processes characters at the beginning and end of the string. If the specified character appears in the middle of the string, it will not be removed.

Clean the spaces or specific characters on the left side of the string:trimLeftFilter

Sometimes, we just need to remove the extra characters from the beginning of a string without affecting the end. At this point,trimLefta filter can come into play.

Basic usage: remove left spaces

withtrimSimilar, without parameterstrimLeftRemove all leading whitespace characters from a string:

{# 移除字符串左侧的所有空白字符 #}
{% set paddedString = "  左侧有空格的字符串 " %}
{{ paddedString|trimLeft }}
{# 输出结果:"左侧有空格的字符串 " #}

Advanced usage: Remove specific characters from the left side

If you want to remove specific characters from the left side of a string, just pass these characters as parameters totrimLeftIt will start matching and removing characters from the left side of the string until it encounters the first non-matching character.

For example, from the string"###重要通知"from###:

{# 移除字符串左侧的指定字符 "#" #}
{% set notice = "###重要通知" %}
{{ notice|trimLeft:"#" }}
{# 输出结果:重要通知 #}

Clean the spaces or specific characters on the right side of the string:trimRightFilter

withtrimLeftOn the contrary,trimRightA filter specifically used to remove excess characters from the right end of a string.

Basic usage: remove spaces from the right side

Without parameterstrimRightIt will remove all whitespace characters from the right end of the string:

{# 移除字符串右侧的所有空白字符 #}
{% set tailString = " 右侧有空格的字符串  " %}
{{ tailString|trimRight }}
{# 输出结果:" 右侧有空格的字符串" #}

Advanced usage: Remove the specific characters from the right

To remove the specific characters from the right side of the string, pass these characters as parameters totrimRightIt will start matching and removing characters from the right side of the string until it encounters the first mismatched character.

For example, from the string"产品型号:AnQiCMS-Pro-Beta---"Remove the trailing-:

{# 移除字符串右侧的指定字符 "-" #}
{% set productModel = "产品型号:AnQiCMS-Pro-Beta---" %}
{{ productModel|trimRight:"-" }}
{# 输出结果:产品型号:AnQiCMS-Pro-Beta #}

Why are these filters important?

These seemingly simple string processing functions play a key role in website operation and content management:

  • Improve user experience:The content on the page is neat and without extra characters, which can make the user's reading more comfortable and enhance the professionalism of the website.
  • Data consistency:When processing content imported from different sources or form data submitted by users, these filters can help us standardize data formats and avoid problems caused by minor differences.
  • URL and SEO Optimization:Remove unnecessary characters or spaces from the URL to generate a more friendly URL structure, which has a positive impact on search engine crawling and ranking.Although AnQiCMS provides the feature of pseudo-static and custom URL, these filters are still indispensable supplements when dealing with some dynamically generated or user input strings.
  • Template Display Logic:In conditional judgments or loops, precise string values can avoid unnecessary errors and ensure the correct execution of template logic.

Additional tip: Remove a specific character from any position in a string:cutFilter

As mentionedtrimThe series of filters mainly acts on the beginning or end of a string. If your need is to remove a stringat any positionthe specific character, thencutThe filter will be a more suitable choice.

cutThe filter will search for and remove all matching substrings, regardless of where they appear.

For example, if you want to remove the string"安-企-C-M-S"all of the-characters:

{# 移除字符串中所有出现的指定字符 #}
{% set hyphenatedString = "安-企-C-M-S" %}
{{ hyphenatedString|cut:"-" }}
{# 输出结果:安企CMS #}

This istrimThe behavior of the filter when processing specific characters is fundamentally different:trimIt removes only at both ends, butcutIt is a replacement over the entire string range (which is actually deletion).

Summary

Mastertrim/trimLeft/trimRightas well ascutThese filters allow you to handle strings with ease in AnQiCMS template creation and content operation, ensuring the content is neat, standardized, and professional.AnQiCMS is a powerful and flexible template engine, which brings us an efficient and convenient content management experience through these practical functions.


Frequently Asked Questions (FAQ)

Q1: Can these filters handle Chinese characters correctly? A1:Yes, AnQiCMS is developed based on Go language and supports UTF-8 encoded characters very well. Therefore,trim/trimLeft/trimRightandcutThese filters can accurately and correctly handle multilingual strings including Chinese characters, without worrying about garbled code or truncation issues.

Q2: How can I check if a string contains a specific character without deleting it? A2:If your goal is to check rather than delete, you can usecontainFilter. For example, you can make conditional judgments in the template like this:

{% set myString = "欢迎使用安企CMS" %}
{% if myString|contain:"安企" %}
  <p>字符串中包含“安企”字样。</p>
{% else %}
  <p>字符串中不包含“安企”字样。</p>
{% endif %}

This will be based onmyStringwhether it includes"安企"to display different content.

Q3:trimDoes the filter still work when removing specific characters if the character is in the middle of the string? A3:No.trim/trimLeftandtrimRightThe filter will only remove specific characters fromboth ends, left or rightStart matching and removing consecutive specified characters. Once an unmatched character is encountered, or a character is inside the string, the operation will no longer continue. If you need to remove the stringat any positionThe specific character, please usecutfilter.

Related articles

How to implement capitalizing the first letter of each word in the article title in AnQiCMS templates?

In website content operation, the standardization of article titles is often a key factor in improving the professionalism and user experience of the website.A neat and consistent title format not only makes the content more attractive but also helps improve the overall image of the website.AnqiCMS provides us with a very concise and efficient solution, which can easily capitalize the first letter of each word in the article title with a simple filter in the template.

2025-11-07

How to force all English text in the AnQiCMS backend custom fields to lowercase?

In website content operation, maintaining consistency in text format is a key step to improving user experience and SEO effects.Sometimes, we may encounter such a need: we hope that the English text input in the custom field of the background, regardless of how the user inputs it in uppercase or lowercase, can be forced to lowercase when displayed on the front-end.This not only makes the page look neater and professional, but also helps avoid SEO problems caused by inconsistent capitalization.

2025-11-07

How to automatically capitalize the first letter of the English name submitted by the user in the AnQiCMS template?

In website operation, we often encounter the need to display user submitted information, such as name.In order for this information to look more professional and unified, it is usually desired that the first letter of the English name be automatically capitalized.This not only enhances the beauty of the user interface, but also ensures the consistency of data display.In the flexible template system of AnQiCMS, implementing this requirement is very simple.

2025-11-07

How to remove only specific types of HTML tags in the AnQiCMS template (such as `<i>` or `<strong>`)?

In the template development of Anqi CMS, we often encounter the need to handle content output by rich text editors.This content usually includes various HTML tags, such as those used for emphasizing `<strong>`, for italicizing `<i>`, or others like `<em>`, `<span>`, `<div>` etc.

2025-11-07

What is the correct syntax for using the `{{ obj|filter__name:param }}` filter in AnQiCMS templates?

AnQiCMS (AnQiCMS) provides strong support for content operations with its efficient and flexible features.In daily content display, we often need to process and format data in a fine-grained manner to ensure that information is presented to visitors in a **state**.At this moment, the 'filter' in the template has become an indispensable tool for us. Today, let's delve into the correct syntax for using filters in Anqi CMS templates: `{{ obj|filter_name:param }}`.Understand and master it

2025-11-07

How to set a default friendly display value for a possibly empty variable in AnQiCMS template?

When building website templates, we often encounter situations where a variable may be empty in the template due to unentered data, optional fields left blank, or specific business logic.If these empty variables are not processed, the front-end page of the website may appear blank areas, display unfriendly placeholders, and even cause layout errors, thereby affecting user experience and the professionalism of the website.

2025-11-07

How to format a 10-digit timestamp retrieved from the database into a custom date-time format in AnQiCMS template?

In website content display, we often encounter time data in a long string of numbers from the database, such as `1678886400`. This string of numbers, which is what we commonly call a timestamp, is very convenient for computers, but it seems cold and difficult to understand for users visiting websites.Fortunately, AnQi CMS provides us with a very convenient feature that can easily convert these original 10-digit timestamps into the date and time format we are accustomed to reading.

2025-11-07

What are the main differences between AnQiCMS's `date` and `stampToDate` filters when handling date data?

In AnQiCMS template development, displaying time data is an indispensable part, whether it is the publication time of articles, the update date of products, or the time records of user behavior, it needs to be presented to the user in a clear and readable manner.To meet the different time processing needs, AnQiCMS provides two core filters: `date` and `stampToDate`.

2025-11-07