In the template development of Anqi CMS, 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,joinThe array elements can be connected using a specified separator to form a new string. Then, when we usesplitAfter splitting a string into an array using a filter,joinDoes the filter concatenate this array back into a string, and will the final result be identical to the original string?This question seems simple, but it actually contains some details worth our in-depth exploration.

Deep understandingsplitFilter

splitThe filter plays the role of parsing structured strings into data lists in the Anqi CMS template.Its basic working principle is to find the specified delimiter and use it to split the string, with each part after the split being an element of the array.

For example, a common scenario is that we have a string of keyword list separated by commas and spaces: 'Anqi CMS, Template Development, Website Operation'. When we use|split:", "When it encounters a delimiter, it gets an array with three elements:)["安企CMS", "模板开发", "网站运营"].

It is worth noting that,splitThe filter handles delimiters very precisely.

  • If your string containsdoes not includeYou specified a delimiter.splitThe filter will return an array containing only the original string as the only element.
  • If your delimiter is aEmpty string("")splitThe filter will split each UTF-8 character in the original string into a separate element. For example, using an empty string to split '你好' will result in["你", "好"].

Deep understandingjoinFilter

joinThe filter is similar tosplitThe operation of the filter is opposite; it receives an array and concatenates all the elements of the array using the delimiter you specify to form a new string.

Follow the above example, if we take["安企CMS", "模板开发", "网站运营"]this array through|join:", "filter processing, we will get the string 'Anqi CMS, Template Development, Website Operation'.

It is especially important to note that,jointhe filter is always.between array elements.This means that if you pass an array containing an empty string (for example,["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?

Understoodsplitandjoin[en]After determining the characteristics, now we can answer this question:In most cases, ifsplitandjoinIf all the literal delimiters used are completely identical, non-empty, and each appears at least once in the original string, then the result will be identical to the original string.

Let's analyze through several specific cases:

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

  2. The string contains consecutive delimitersAssuming the original string is"apple,,banana,,,orange", the delimiter is a comma",".|split:","It will split it into["apple", "", "banana", "", "", "orange"]. Use again|join:","Concatenate it back together, the result is"apple,,banana,,,orange"[en] This is still consistent with the original string. This is becausesplitwill recognize empty content between consecutive delimiters as empty string elements, [en]joinThe elements are restored accurately in the concatenation, and separators are inserted 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:","Join back together, as the array has only one element,jointhe separator will not be inserted, the result is still,"AnQiCMS"the same as the original string.

  4. Using an empty string as the separator forsplitassuming the original