Articles in this section

How to separate multiple pieces of text using StringSplit()

This article explains what the StringSplit() function is and provides examples of how it can be used with the binding syntax in smart templates.

  Prerequisites

  • Library and at least one Dynamics module enabled.
  • Templafy Desktop and Office VSTO add-in installed.
  • Space owner access to the Templafy tenant.

What is the StringSplit() function?

StringSplit() is a function that separates text with a separator in between into multiple pieces. 

StringSplit() logic

Syntax {{StringSplit(Separator, Value)[Index number]}}
Input Separator, text and (optional) index number
Output Separated text without the separator itself

  Important

Array indexes start with 0. So index number [0] returns the first item, index number [1] returns the second item and so on.

StringSplit() function examples

Example 1

Using the StringSplit() function to swap the first name and last name (and remove the comma) of the user profile when the input is '<Last name>, <First name>'.

Binding {{StringSplit(", ", UserProfile.DisplayName)[1]}} {{StringSplit(", ", UserProfile.DisplayName)[0]}}
Input "Johnson, John"
Output "John Johnson"

On the Template tab the binding looks like this:

Stringsplit binding template designer.png

  Tip

If you only want to show the first name as output, then use the binding {{StringSplit(", ", UserProfile.DisplayName)[1]}}.
Replace the [1] by [0] if you only want to show the last name.

Example 2

Using the StringSplit() function to create a bulleted list. The input is a fixed number of values.

  1. On the Form tab create a form field of Type Text named 'Color'.
  2. In the template insert a bullet and a placeholder '[Color]'. Select the placeholder:
    wd_placeholdercolor.png
  3. On the Template tab click + Add smart field. Go to Form responses and select 'Color' . In the Advanced section create the binding {{StringSplit(", ", Form.Color)[0]}} in the Binding field.
  4. Press Enter to go to the next line and create the binding {{StringSplit(", ", Form.Color)[1]}}.
  5. On the next line create the binding {{StringSplit(", ", Form.Color)[3]}}.
  6. On the next line create the binding {{StringSplit(", ", Form.Color)[2]}}.
    The result looks like this:
    Stringsplit binding template designer bullet.png
  7. Save the template and upload it to the Admin Center.
  8. Now when a user creates a document and types 'red, green, blue, orange' as value (note that the values are separated by a comma), the result will be a bulleted list like this:
    wd_bulletedlist.png
    Note that the output is 'red, green, orange, blue'. This is because the order of the index numbers used in the bindings is [0], [1], [3], [2], not [0], [1], [2], [3]!

  Note

  • This binding always expects 4 values as input! If the input is e.g. 'red, green, blue' (so only 3 values) then the third bullet will show an empty value!
  • If you want to support a variable input (so no empty bullet is shown) you need to use an adaptive section (repeating group) as shown in example 3 below.

Example 3

Using the StringSplit() function to create a bulleted list. The input is a variable number of values.

In this example the values are inserted using a HostSystem binding, from a file with comma separated values (CSV).

  1. In the template insert a bullet and a placeholder '[Color]'. Select the placeholder:
    wd_placeholdercolor.png
  2. On the Template tab click + Add smart field and select Custom repeating group.
  3. In the Collection Expression field create the binding {{StringSplit(";", HostSystem.Color)}}. The result looks like this:
    Stringsplit binding template designer Collection Expression.png
  4. Click Add to template.
  5. On the Developer tab in the Office ribbon enable Design Mode:

    wd_designmode.png
  6. In the template select the placeholder text '[Color]':
    wd_contentcontroldesignmode.png
  7. On the Template tab click + Add smart field and select Custom text binding.
  8. In the Binding field create the binding {{CurrentItem}}. The result looks like this:
    Stringsplit binding template designer current item.png
  9. Click Add to template. The result looks like this:
    Stringsplit binding adaptive section.png
  10. Save the template and upload it to the Admin Center.
  11. Now when a user creates a document the result looks like this:
    Binding {{StringSplit(";", HostSystem.Color)}}
    Input HostSystem.Color = "red;green;blue" HostSystem.Color = "red;green;blue;orange;yellow;purple"
    Output wd_adaptivesectionthreecolors.png wd_adaptivesectionsixcolors.png

  Important

  • When a template contains an adaptive section (repeating group) the Document content updater is not supported.
  • If the separator is not found then the output is shown on one line.
percentage prefill text tech_role
Was this article helpful?
0 out of 0 found this helpful

Comments

0 comments

Article is closed for comments.