In AnQi CMS template development, we often encounter scenarios where string processing is required.splitandjoinFilters are very commonly used tools for handling such needs.splitCan split a string into an array using a specified delimiter, andjoinIt can concatenate the elements of an array with a specified delimiter to recombine them into a string. Then, when we usesplita filter to split a string into an array, and then usejoinDoes the filter concatenate this array back into a string, will the final result be exactly the same as the original string?This question seems simple, but it actually contains some details that are worth our in-depth exploration.

Deep understandingsplitFilter

splitThe filter plays a role in the Anqi CMS template in parsing structured strings into data lists.Its basic working principle is to find the delimiter you specify and break the string at that point, using the broken parts as array elements.

For example, a common scenario is that we have a string of keywords separated by commas and spaces: |split:", "When a delimiter is a comma and space, it will get an array containing three elements:["安企CMS", "模板开发", "网站运营"].

It is worth noting that,splitThe filter is very precise in handling delimiters.

  • If there is a string in yourDo not includeThe delimiter you specified,splitThe filter will return an array that contains only the original string as the sole element.
  • If your delimiter is aempty string("")splitThe filter will split each UTF-8 character of the original string into an independent element. For example, if you split the word ”你好” with an empty string, you will get["你", "好"].

Deep understandingjoinFilter

joinThe filter then withsplitThe filter operation is the opposite, it receives an array and concatenates all the elements of the array with the specified delimiter to form a new string.

Follow the example above, if we take["安企CMS", "模板开发", "网站运营"]this array through|join:", "filter processing, we will get the string "AnQi CMS, template development, website operation".

It should be particularly noted that,joinThe filter is alwaysbetween array elementsinserting a separator. This means that if you pass an array containing an empty string (such as["a", "", "b"])joinThe separator will be inserted between these empty string elements to retain the original structure.

Will the result be exactly the same as the original string?

UnderstoodsplitandjoinAfter its characteristics,In most cases, ifsplitandjoinAll the identical, non-empty, and at least once appearing literal delimiters used in the original string will result in the original string being exactly the same.

Let's analyze through several specific situations:

  1. Ideal situation: same non-empty delimiter and the delimiter existsFor example, the original string is"Hello, 99, 3.140000, good". Use|split:", "Get["Hello", "99", "3.140000", "good"]. Use again|join:", "Glue back together, the result is still the same"Hello, 99, 3.140000, good"It is completely consistent with the original string. The documentation of AnQi CMS also provides such an example, proving the consistency of the result under common usage.

  2. The original string contains consecutive delimitersAssuming the original string is"apple,,banana,,,orange"The delimiter is a comma",".|split:","It will be split into["apple", "", "banana", "", "", "orange"]. Use again|join:","Pasted back together, the result is"apple,,banana,,,orange"It remains consistent with the original string. This is becausesplitWill recognize empty content between consecutive delimiters as empty string elements,joinWhen concatenating, it will accurately restore these empty elements and insert separators between them.

  3. The original string does not contain separatorsIf the original string is"AnQiCMS", and the separator is",".|split:","You will get["AnQiCMS"]. Use again|join:","glue back together, since the array has only one element,jointhe separator is not inserted, the result is still"AnQiCMS"the same as the original string.

  4. using an empty string as a separator to performsplitassuming the original