Remove all leading and trailing spaces and specific characters from a string

How to remove leading and trailing spaces or specific characters from a string in Anqi CMS template?

trim/trimLeft/trimRightThe filter can separately remove leading and trailing spaces or specific characters from a string.

  • trimThe filter can remove leading and trailing spaces or specific characters from a string;
  • trimLeftThe filter can remove leading spaces or specific characters from a string;
  • trimRightThe filter can remove trailing spaces or specific characters from a string.

Usage method

trimFilter usage

# 仅删除空格、换行符
{{obj|trim}}
# 或者 删除指定字符
{{obj|trim:关键词}}

For example, to remove a string欢迎使用安企CMSThe leading and trailing spaces, then you can write it like this:

{{" 欢迎使用安企CMS "|trim}}
# 显示结果
欢迎使用安企CMS

Remove the string欢迎使用安企CMSThe leading and trailing characters欢迎CMSIt can be written like this:

{{"欢迎使用安企CMS"|trim:"欢迎CMS"}}
# 显示结果
使用安企

trimThe filter also supports using the assignment tag (set) to store the processing result in a variable, such as:

{% set source = "欢迎使用安企CMS(AnQiCMS)" %}
{% set result = source|trim:"欢迎" %}
{{result}}
# 显示结果
使用安企CMS(AnQiCMS)

trimLeftFilter usage

# 仅删除空格、换行符
{{obj|trimLeft}}
# 或者 删除指定字符
{{obj|trimLeft:关键词}}

For example, to remove a string欢迎使用安企CMSLeading spaces, it can be written like this:

{{" 欢迎使用安企CMS "|trimLeft}}
# 显示结果
欢迎使用安企CMS

Remove the string欢迎使用安企CMSThe leading character欢迎It can be written like this:

{{"欢迎使用安企CMS"|trimLeft:"欢迎"}}
# 显示结果
使用安企CMS

trimLeftThe filter also supports using the assignment tag (set) to store the processing result in a variable, such as:

{% set source = "欢迎使用安企CMS(AnQiCMS)" %}
{% set result = source|trimLeft:"欢迎" %}
{{result}}
# 显示结果
使用安企CMS(AnQiCMS)

trimRightFilter usage

# 仅删除空格、换行符
{{obj|trimRight}}
# 或者 删除指定字符
{{obj|trimRight:关键词}}

For example, to remove a string欢迎使用安企CMSLeading spaces, it can be written like this:

{{" 欢迎使用安企CMS "|trimRight}}
# 显示结果
欢迎使用安企CMS

Remove the string欢迎使用安企CMSThe leading character欢迎It can be written like this:

{{"欢迎使用安企CMS"|trimRight:"CMS"}}
# 显示结果
欢迎使用安企

trimLeftThe filter also supports using the assignment tag (set) to store the processing result in a variable, such as:

{% set source = "欢迎使用安企CMS(AnQiCMS)" %}
{% set result = source|trimRight:")" %}
{{result}}
# 显示结果
欢迎使用安企CMS(AnQiCMS

Example Demonstration

{{" 欢迎使用安企CMS(AnQiCMS) "|trim}}
# 显示结果
欢迎使用安企CMS(AnQiCMS)
{{"欢迎使用安企CMS(AnQiCMS)"|trim:"欢迎"}}
# 显示结果
使用安企CMS(AnQiCMS)
{{" 欢迎使用安企CMS(AnQiCMS) "|trimLeft}}
# 显示结果
欢迎使用安企CMS(AnQiCMS)
{{"欢迎使用安企CMS(AnQiCMS)"|trimLeft:"欢迎"}}
# 显示结果
使用安企CMS(AnQiCMS)
{{"欢迎使用安企CMS(AnQiCMS) "|trimRight}}
# 显示结果
欢迎使用安企CMS(AnQiCMS)
{{"欢迎使用安企CMS(AnQiCMS) "|trimRight:") "}}
# 显示结果
欢迎使用安企CMS(AnQiCMS