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
|
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 |
ImportantArray 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:
TipIf you only want to show the first name as output, then use the binding |
Example 2
Using the StringSplit() function to create a bulleted list. The input is a fixed number of values.
- On the Form tab create a form field of
Type
Text named 'Color'.
- In the template insert a bullet and a placeholder '[Color]'. Select the placeholder:
-
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. - Press Enter to go to the next line and create the binding
{{StringSplit(", ", Form.Color)[1]}}
. - On the next line create the binding
{{StringSplit(", ", Form.Color)[3]}}
. - On the next line create the binding
{{StringSplit(", ", Form.Color)[2]}}
.
The result looks like this: - Save the template and upload it to the Admin Center.
- 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:
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
|
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).
- In the template insert a bullet and a placeholder '[Color]'. Select the placeholder:
- On the Template tab click + Add smart field and select Custom repeating group.
- In the Collection Expression field create the binding
{{StringSplit(";", HostSystem.Color)}}
. The result looks like this: - Click Add to template.
- On the Developer tab in the Office ribbon enable Design Mode:
- In the template select the placeholder text '[Color]':
- On the Template tab click + Add smart field and select Custom text binding.
- In the Binding field create the binding
{{CurrentItem}}
. The result looks like this: - Click Add to template. The result looks like this:
- Save the template and upload it to the Admin Center.
- 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
Important
|
Comments
Article is closed for comments.