How to extract part of the numbers from a long ID string for display or logical judgment?

Calendar 👁️ 76

In website operation, we often encounter the need to handle various long string IDs, such as product SKUs, order numbers, user IDs, and so on.These ID strings usually contain multiple segments of information, and we may only need a part of the numbers for displaying on the page, statistical reports, or for some business logic judgments.AnQiCMS (AnQiCMS) powerful template engine provides us with flexible tools, capable of easily meeting such needs.

The string processing tool in Anqi CMS template: Filters

AnQiCMS's template engine is built-in with a rich set of 'filters' (Filters), which are like little tools that can perform various processing on variables in the template, including string truncation, formatting, conversion, etc.To extract the part of the number from a long ID string, we will mainly rely on these filters.

Core strategy one: Accurate extraction, extract the required numbers (sliceFilter)

sliceThe filter is the most direct and effective method for processing long strings. It allows you to extract any part of the string based on the starting and ending positions.

Working principle: sliceThe filter goes throughobj|slice:"start:end"in the form to use.

  • obj: Represents the long ID string variable you want to operate.
  • "start:end": Specifies the range to be截取.startIs the starting index (counting from 0),endIs the ending index (does not include the character at this position).

Useful scenario example:Assuming your order ID string is:ORDER-20231026-VIP12345-001where:

  • ORDER-Is a fixed prefix:
  • 20231026Is a date:
  • VIP12345Is a member code:
  • 001Is the order serial number

1. Extract the order serial number “001”:Serial number "001" is located at the last three digits of the entire string. We can start extracting from a specific index. To extract001, it is inORDER-20231026-VIP12345-001The starting index is25(The position of '0', the end index is)28(Not included, i.e., the end of the string).

{% set order_id = "ORDER-20231026-VIP12345-001" %}
{% set serial_number = order_id|slice:"25:28" %}
<div>订单流水号:{{ serial_number }}</div>

output:订单流水号:001

2. Extract the member code '12345':Member code "VIP12345" is located in the middle of the string. We can first extract the part containing "VIP", then process it. To extract12345, it is inORDER-20231026-VIP12345-001The starting index is19(The position of 'V'), the ending index is25(The next position of '5').

{% set order_id = "ORDER-20231026-VIP12345-001" %}
{% set vip_code_with_prefix = order_id|slice:"19:25" %} {# 截取 "VIP12345" #}
{% set vip_number_part = vip_code_with_prefix|slice:"3:" %} {# 从 "VIP" 后面开始截取 "12345" #}
<div>会员编号:{{ vip_number_part }}</div>

output:会员编号:12345

  • Tip: sliceThe filter is very flexible,startorend Anything can be omitted. For example,obj|slice:"5:" It will start from index 5 to the end of the string;obj|slice:":10"It will take from the beginning to index 9.

Core strategy two: Extracted number: Used for display and logic judgment

Once you pass throughsliceThe filter successfully extracted the required numerical part, which can then be used for display or logical judgment.

1. For display:Simply output the variable in the template.

{% set order_id = "ORDER-20231026-VIP12345-001" %}
{% set serial_number = order_id|slice:"25:28" %}
<p>您的订单号是:{{ order_id }},流水号为:<span style="font-weight: bold;">{{ serial_number }}</span></p>

output:您的订单号是:ORDER-20231026-VIP12345-001,流水号为:001

2. Used for logical judgments:If you need to compare the extracted numbers numerically (for example, to determine size), you first need to useintegerThe filter converts it from a string to an integer. The Anqi CMS template engine supports basic logical judgment tags

Related articles

How to perform complex mathematical expression calculations in templates, such as exponentiation or multi-step operations?

In website content management, we often need to handle various data, and this data is not always in its final form.Sometimes, we need to perform simple addition, subtraction, multiplication, and division in templates, and sometimes even more complex mathematical operations, such as calculating squares, cubes, or handling complex expressions with multiple steps.For AnQiCMS users, the good news is that its template engine provides very flexible and powerful mathematical calculation capabilities, allowing us to easily meet these needs.The template system of AnQiCMS is based on Django template engine syntax

2025-11-08

How does `floatformat` prevent the display of the decimal part when a floating-point number is exactly an integer (such as 10.00)?

In website content operation, we often encounter situations where we need to display numbers.These numbers may come from a floating-point type in a database, but in some cases, when they are exactly integers (such as `10.00`, `50.0`), we want them to be presented in a more concise integer form, such as `10` or `50`, rather than with unnecessary decimal parts.AnQiCMS (AnQiCMS) provides powerful template tags and filters, where the `floatformat` filter is a powerful tool to solve such problems

2025-11-08

How to dynamically adjust the decimal precision of displayed numbers in a template?

In website content operation, the precise display of numbers is often crucial, whether it is product prices, statistical data, or technical indicators, the accuracy of decimal point display directly affects the accuracy of information and the understanding of users.AnQiCMS provides a flexible template engine that allows us to easily adjust the decimal precision of numbers in frontend templates, thus satisfying various complex display requirements.The AnQiCMS template system is developed based on Go language, with a syntax style similar to Django, it offers powerful Filter (Filter) functions

2025-11-08

How can I convert a string price from a custom field into a number that can be used in the order total in the template?

In website operation, we often encounter the need to handle numerical data such as product prices or service charges.This data is sometimes stored in custom fields, but for various reasons, they may be saved in the form of strings (text), such as "199.50 yuan" or "150".When we need to perform a total calculation on these prices in a template (such as the order total), directly performing a 'sum' operation on the string will yield unexpected results (such as '199.5015.00' instead of '214.50').At this point, it is necessary to convert these string prices into actual numbers

2025-11-08

How does the Anqi CMS template handle numeric data that may be received as strings from external APIs?

In website operation, we often encounter situations where we need to integrate data from different external APIs.These APIs provide data of various types and formats, and they are quite different, especially when dealing with numerical data. We may find that fields such as "price", "stock", or "rating", which should be numeric, are returned as strings.This not only brings trouble to the template display, it may also affect data calculation and subsequent processing.AnQi CMS, with its flexible template engine and rich data processing capabilities, provides us with an elegant solution

2025-11-08

How can percentage data be displayed on a webpage and ensure the correct formatting of decimal places?

In website operation, we often need to display various data, among which percentage data is particularly common.Whether it is the completion, growth rate, discount information or other statistics, it is crucial to present percentages clearly and accurately, and ensure uniformity in the format of decimal places, which is essential for improving user experience and website professionalism.AnQiCMS (AnQiCMS) provides a flexible template system, which can be very convenient to meet this requirement with its powerful filter function.### Data storage: the foundation is crucial In AnQi CMS

2025-11-08

I want to convert the inventory quantity (string) to an integer for comparison, which filter should I use?

In AnQi CMS template development, we sometimes encounter situations where we need to compare data such as product inventory quantity.These quantities may be stored as strings in the content model, for example, a custom field `Stock` of a product model may store such texts as "100", "50", or even "0.5".However, simple string comparison does not meet our needs when performing numerical comparisons, for example, the string "10" will be smaller than the string "2" in string comparison (because '1' is smaller than '2'), which is obviously not consistent with the actual size of the numbers

2025-11-08

How can I avoid scientific notation when displaying floating-point numbers directly in a template?

In the AnQiCMS template, when we need to display floating-point numbers, we sometimes encounter them appearing in scientific notation, such as `1.23E+07` instead of `12,300,000`.This display method is useful in some specific scenarios, but it is often difficult for ordinary users to understand intuitively, affecting the aesthetics and readability of the page, especially in situations where the accuracy is not high but the intuitiveness is required, such as displaying product prices, statistics, etc.AnQiCMS's template engine design borrowed from Django syntax

2025-11-08