How to delete multiple specified character sets from both ends of a string in AnQiCMS template, for example, `()` or `[]` and other brackets?

Calendar 👁️ 72

In AnQiCMS template, flexible string handling is an important factor in improving the display effect of content and data accuracy. We often encounter the need to remove specific character sets from both ends of a string, for example, when retrieving titles or tags from a database, they may be()/[]even<>Parentheses enclosed, and these parentheses may not be needed when displayed on the front end.Fortunately, AnQiCMS is developed based on the Go language, its template engine adopts the Django-like template syntax, providing powerful filter functions, which can easily handle such string processing tasks.

Get to know the string processing in AnQiCMS templates

The template system of AnQiCMS is designed to be simple and efficient, allowing developers to{{ 变量 }}and control logic by|Symbols combined with filters to process data.These filters are like individual processing stations in a data pipeline, capable of formatting, truncating, replacing, and other operations.To remove the specified character set from both ends of a string, we need to use one of the very practical filters -trim.

Core solution:trimFilter

trimThe filter is used to remove the specified characters from the beginning and end of a string.The principle of its operation is to detect whether the characters at both ends of the string are included in the "character set" parameter you provide. If they match, they are removed, and this process continues until a character not in the character set is encountered.

Basic usage: Remove single type of bracket

Suppose we have a string.(AnQiCMS)We want to remove the parentheses at both ends.trimThe filter can do this easily. You just need to pass the characters to be removed as parameters to it:

{% set my_string = "(AnQiCMS)" %}
<p>原始字符串:{{ my_string }}</p>
<p>移除圆括号后:{{ my_string|trim:"()" }}</p>

The output will be:

原始字符串:(AnQiCMS)
移除圆括号后:AnQiCMS

Similarly, if the string is[AnQiCMS]the operation to remove brackets is also consistent:

{% set my_string = "[AnQiCMS]" %}
<p>原始字符串:{{ my_string }}</p>
<p>移除方括号后:{{ my_string|trim:"[]" }}</p>

The output will be:

原始字符串:[AnQiCMS]
移除方括号后:AnQiCMS

Advanced usage: remove multiple or nested brackets

trimThe power of the filter lies in the ability to specify a 'character set' parameter containing multiple characters.It removes all instances that match these characters from both ends of the string, regardless of their order.This means that even nested parentheses, as long as they are at the outermost layer of the string,trimcan also be processed at one time.

For example, if the string is[(AnQiCMS)]we want to remove brackets and parentheses at the same time:

{% set my_string = "[(AnQiCMS)]" %}
<p>原始字符串:{{ my_string }}</p>
<p>移除方括号和圆括号后:{{ my_string|trim:"()[]" }}</p>

Here()[]is a character set,trimit will remove all from both ends(,),[,]The character, until the start of the valid content of the string. The output is:

原始字符串:[(AnQiCMS)]
移除方括号和圆括号后:AnQiCMS

Consider a continuous nested scenario, such as((AnQiCMS)):

{% set my_string = "((AnQiCMS))" %}
<p>原始字符串:{{ my_string }}</p>
<p>移除连续括号后:{{ my_string|trim:"()" }}</p>

due totrimIt will continue to remove matching characters until it encounters a non-matching character, therefore, even if the brackets are nested in multiple layers, as long as they are both at both ends, they will be completely removed, and the output will be:

原始字符串:((AnQiCMS))
移除连续括号后:AnQiCMS

Other related filters (辅助说明)

AlthoughtrimThe filter mainly processes the strings at both ends, but sometimes you may also need to handle specific characters within the string. In this case,replaceThe filter will be a good helper for you.

replaceFilter: Replace characters inside a string.

replaceThe filter is used to replace all occurrences of a specific substring in a string with another substring. It accepts two parameters, separated by a comma,: the old substring and the new substring.

For example, if the string isAnQi(CMS)系统We want to remove the middle one,():

{% set my_string = "AnQi(CMS)系统" %}
<p>原始字符串:{{ my_string }}</p>
<p>移除内部括号后:{{ my_string|replace:"(,"|replace:")," }}</p>

Here we need to use it twice in a chain,replaceFilter, replace once,(Empty, replace again,)Empty. The output is:

原始字符串:AnQi(CMS)系统
移除内部括号后:AnQiCMS系统

This istrimThe filter only processes strings with contrasting ends, each with its own focus, and can be flexibly combined according to actual needs.

Application scenarios and precautions

trimThe filter is widely used in AnQiCMS templates, for example:

  • Display data cleaningWhen the title, description, and other content obtained from the backend may contain unnecessary decorative characters, usetrimIt can be presented beautifully on the front end.
  • Uniform formatEnsure that all entries in the tag cloud or keyword list are displayed in a uniform format without any extra brackets.
  • SEO optimizationSometimes the backend data may contain extra symbols when processing SEO titles or descriptions, the frontend cantrimquickly clear them through the filter.

UsetrimNote on the filter:

  • trimThe filter only processes the outermost layer of the string. If the brackets are in the middle of the string, they will not be removed. For exampleAnQi(CMS)系统Use|trim:"()"It will still be obtainedAnQi(CMS)系统.
  • trimThe parameter is a "character set", which means it will match and remove the characters in this setanythat appear, rather than as a whole "substring" for matching. For example|trim:"()means to remove all(and all)characters, not just to remove()this sequence.

MasteredtrimThe use of filters and their配合 with other string processing tools will allow you to manage and display content more efficiently and flexibly in the AnQiCMS template, ensuring the neatness and beauty of the website data.


Frequently Asked Questions (FAQ)

Q1:trimCan the filter remove parentheses from the middle of a string?A1: No.trimThe filter is designed to remove stringsBeginning and endThe specified character. If you need to remove the parentheses from a stringmiddleYou should usereplaceFilter. For example, to remove"AnQi(CMS)系统"from()you can use{{ "AnQi(CMS)系统"|replace:"(,"|replace:")," }}.

Q2: If a string is((AnQiCMS)), usingtrim:"()"what will you get?A2: Will getAnQiCMS.trimThe filter will continue to remove characters from both ends of the string that match the specified character set until it encounters a non-matching character. So,((and))they will all be removed consecutively.

Q3:trimHow does the filter handle Chinese characters?A3:trimThe filter handles Chinese characters in the same way as English characters because it operates on a character set. If you aretrimThe parameter contains Chinese characters, it will try to remove these Chinese characters from both ends of the string. For example,{{ "【测试】"|trim:"【】" }}It will output测试The AnQiCMS template engine has good support for Unicode characters.

Related articles

What are the specific differences and application scenarios of the `trim`, `trimLeft`, and `trimRight` filters in removing characters from the beginning and end of a string?

In Anqi CMS template development, in order to better control the display of content, we often need to process strings, such as removing extra spaces, removing unnecessary characters, etc.At this point, `trim`, `trimLeft`, and `trimRight` these three filters are particularly important.They are your good assistants for content formatting and data cleaning, let's take a look at their respective features and application scenarios.### `trim` Filter: Bidirectional Trimming, All-in-One Cleaning `trim` Filter is the most commonly used and comprehensive one

2025-11-08

How can you remove the extra newline characters and spaces at the beginning of a paragraph in the content of an article using which `trim` filter series in the AnQiCMS template?

In the daily operation of websites, we often encounter some minor flaws in the content of articles after they are published, such as extra line breaks or spaces at the beginning and end of paragraphs, which not only affects the beauty of the page, but sometimes also leads to inconsistent layout.AnQiCMS as an efficient content management system takes full consideration of these details and provides convenient solutions in its powerful template engine.Today, let's delve into the `trim` filter series used in the AnQiCMS template to remove these redundant blanks.### Say goodbye to layout troubles

2025-11-08

How to ensure that each tag does not contain leading or trailing spaces or delimiters when managing keyword tags?

When using AnQi CMS to manage website content, keyword tags are undoubtedly an important tool for enhancing content discoverability and SEO performance.They help us organize content, making it easier for search engines and users to understand the core theme of the article.However, in daily operations, we may encounter some minor troubles, such as accidentally including extra spaces or separators when entering keyword tags, which may not only affect the accuracy of the tags but also lead to inconsistencies in data.How can we effectively avoid and handle these user mistakes to ensure that each keyword tag is clean and tidy?

2025-11-08

How to remove special symbols (such as “/” or “-”) at the end of a custom URL alias without affecting the middle content of the string?

In website operation, a clear and standardized URL is crucial for search engine optimization (SEO) and user experience.AnQiCMS (AnQiCMS) provides flexible custom URL alias functionality, allowing us to optimize the link structure according to our needs.However, sometimes due to manual input or certain specific situations, custom URL aliases may end with some unnecessary special characters, such as slashes (`/`) or hyphens (`-`), which may make the URL look unattractive and even affect the SEO standards in some cases.

2025-11-08

How to automatically remove leading and trailing newline and tab characters from the `Description` field when the template is output?

In website content operation, the importance of the `Description` field on the page is self-evident.It is not only one of the key pieces of information that search engines use to crawl and understand page content, but also a window for users to initially understand the theme of the page in search results.A clear, concise, and non-redundant description that can effectively improve click-through rate and have a direct impact on SEO performance.However, in actual operation, we sometimes find that even though the page description is carefully written in the background, when it is output in the front-end template

2025-11-08

How to remove leading or trailing non-visible characters from the string data obtained from the content acquisition module in the template?

When using AnQi CMS for website content operations, the content collection module is undoubtedly an important way to obtain a large amount of information and quickly enrich the website content.However, text data collected or imported from external sources often contains unnecessary leading or trailing spaces, newline characters, or even other invisible characters.These seemingly minor 'dirty data' can affect the aesthetics of page layout, content alignment, and even cause subtle negative impacts on search engine optimization (SEO).

2025-11-08

How to get the category description with a specified ID in Anqi CMS template and truncate it?

In the Anqi CMS template, effectively managing and displaying category descriptions is a common requirement.Especially when displayed on list pages or block displays, we often need to show a concise introduction of the category rather than a full-length description.This article will provide a detailed introduction on how to obtain the specified ID category description in Anqi CMS template and perform appropriate truncation processing to ensure that the content is both concise and beautiful.Get category description by specified ID In AnQi CMS template, getting category details mainly depends on the `categoryDetail` tag

2025-11-08

How to get the website name in Anqi CMS template and correctly concatenate it in the SEO title?

In website operation, a well-constructed SEO title is crucial for attracting user clicks and improving search engine rankings.It must not only accurately summarize the page content but also ingeniously integrate the brand information.For AnQiCMS users, how to dynamically obtain the website name in the template and correctly concatenate it into the SEO title is a practical and efficient skill.AnQiCMS as a content management system that focuses on SEO optimization, provides very flexible tags at the template level, allowing you to easily achieve this goal.

2025-11-08