Articles in this section

How to join multiple pieces of text with a separator using StringJoin()

About this article

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

 

Prerequisites

 

 

What is the StringJoin() function? 

StringJoin() is a commonly used function that joins multiple pieces of text together with a separator in between. 

 

StringJoin() logic

Syntax {{StringJoin(Separator, Value1, Value2, Value3...)}}
Input Separator and text
Output Joined text with the separator in between

 

 

StringJoin() function examples

 

Example 1

Using the StringJoin() function to join two pieces of text with a dash in between.

Binding {{StringJoin("-", "text1", "text2")}}
Output "text1-text2"

 

 
  • If "-" or "_" are used as separator with the "Title" document property, the name will be truncated by Word and output will result in "text1.docx" using the example above

 

Example 2

Using the StringJoin() function to join three pieces of text with a slash in between.

Binding {{StringJoin("/", "text1", "text2", "text3")}}
Output "text1/text2/text3"

 

Example 3

Using the StringJoin() function together with the "Separator" question originating from the response form where the user will be able to define the desired separator that should separate the address.

Binding {{StringJoin(Form.Separator, Form.Street, Form.City, Form.Zipcode, Form.Country)}}
Input Form.Separator = "-" Form.Separator = "/"
Output "123 Washington Rd.-Lake Forest-60045-USA" "123 Washington Rd./Lake Forest/60045/USA"

 

Example 4

Using the StringJoin() function together with the "Zipcode" question originating from the response form, if the Zipcode question isn't filled by the user, the separator will be skipped.

Binding {{StringJoin(" | ", Form.Street, Form.City, Form.Zipcode, Form.Country)}}
Input Form.Zipcode = "" Form.Zipcode = "60045"
Output "123 Washington Rd. | Lake Forest | USA" "123 Washington Rd. | Lake Forest | 60045 | USA"

 

Example 5

Using the StringJoin() function together with the FormatDateTime() function.

Binding {{StringJoin(" | ", FormatDateTime(Form.Date, "dd/MM/yyyy", "en-US"), Form.Title, Form.Name)}}
Input

Form.Date = "2023-3-17"

Form.Title = "Report"

Form.Name = "HR"

Form.Date = ""

Form.Title = "Report"

Form.Name = "HR"

Output "17/03/2023 | Report | HR" "Report | HR"

 

 
  • Note that {{StringJoin(" | ", Form.Date, Form.Title, Form.Name)}} will not work.
    You need to use the FormatDateTime() function, because the parameter needs to be of type String (expression Form.Date evaluates to type DateTime).

 

Example 6

Using the StringJoin() function together with IfElse() to only insert the address if the "IncludeAddress" checkbox question originating from the response form is checked.

Binding {{IfElse(Form.IncludeAddress, StringJoin(" | ", Form.Street, Form.City, Form.Zipcode, Form.Country), "")}}
Input Form.IncludeAddress = "☑" Form.IncludeAddress = ""
Condition True False
Output "123 Washington Rd. | Lake Forest | 60045 | USA" Empty content control

 

Example 7

Using the IfElse() function in combination with StringJoin() to hide text (using a Group element) if both questions 1 and 2 are empty.

Binding {{IfElse(Equals(StringJoin("", Form.Question1, Form.Question2), ""), VisibilityType.Hidden, VisibilityType.Visible)}}
Input

Form.Question1 = ""

Form.Question2 = ""

Form.Question1 = "Hello world"

Form.Question2 = ""

Output Hidden text Visible text

 

Example 8

Using a nested StringJoin() function together with IfElse() to show plain text ("E:", "P:" and "W:") combined with the value originating from the user profile.

Binding {{StringJoin(" | ", StringJoin(" ", IfElse(Equals(UserProfile.Email, ""), "", "E:"), UserProfile.Email), StringJoin(" ", IfElse(Equals(UserProfile.Phone, ""), "", "P:"), UserProfile.Phone), StringJoin(" ", IfElse(Equals(UserProfile.Website, ""), "", "W:"), UserProfile.Website))}}
Input

UserProfile.Email = "info@test.com"

UserProfile.Phone = ""

UserProfile.Website = "www.test.com"

UserProfile.Email = "info@test.com"

UserProfile.Phone = "+31401234567"

UserProfile.Website = "www.test.com"

Output E: info@test.com | W: www.test.com

E: info@test.com | P: +31401234567 | W: www.test.com

 

Example 9

Using a nested StringJoin() function in combination with IfElse() to show plain text depending on the document language, combined with the value of the user profile field.

This is the same binding used in example 7, but with the plain text ("E:", "P:" and "W:") replaced by the Translate() function. The Translations data source looks like this:

 

Translations data source

translations.png

 

Binding {{StringJoin(" | ", StringJoin(" ", IfElse(Equals(UserProfile.Email, ""), "", Translate("EmailShort", DocumentLanguage)), UserProfile.Email), StringJoin(" ", IfElse(Equals(UserProfile.Phone, ""), "", Translate("PhoneShort", DocumentLanguage)), UserProfile.Phone), StringJoin(" ", IfElse(Equals(UserProfile.Website, ""), "", Translate("WebsiteShort", DocumentLanguage)), UserProfile.Website))}}
Input

UserProfile.Email = "info@test.com"

UserProfile.Phone = "+31401234567"

UserProfile.Website = "www.test.com"

DocumentLanguage = "en-US"

UserProfile.Email = "info@test.com"

UserProfile.Phone = "+31401234567"

UserProfile.Website = "www.test.com"

DocumentLanguage = "nl-NL"

Output E: info@test.com | P: +31401234567 | W: www.test.com

E: info@test.com | T: +31401234567 | W: www.test.com

 

Example 10

Using the StringJoin() function with separators will always display all the values on one line. To show each value on a separate line, you can use the escape sequence \n to create a new line. However,  \n cannot be typed directly in Template Designer. Instead, specific steps must be followed in Word and PowerPoint. 

In Word: 

  1. Create a binding using StringJoin().
    For example: {{StringJoin(" ", UserProfile.Email, UserProfile.Phone, UserProfile.Website)}}
  2. Open the properties of the content control from the Developer tab.
  3. In the Tag field, change {{StringJoin(\" \", to {{StringJoin(\"\n\",.
  4. The result looks like this:

contentcontrolproperties.png

 

 
  • Note that \n will not be displayed in the Template Designer.

 

In PowerPoint:

  1. Create a binding on a shape using StringJoin().
    For example: {{StringJoin(" ", UserProfile.Email, UserProfile.Phone, UserProfile.Website)}}
  2. Right-click the Shape and select Edit Alt Text.
  3. In the Alt Text task pane, change the part {{StringJoin(\" \", to {{StringJoin(\"\n\",.
  4. The result looks like this:

editalttext.png

 

Binding
(Tag in Word / Alt Text in PowerPoint)
{"templafy":{"type":"text","binding":"{{StringJoin(\"\n\", UserProfile.Email, UserProfile.Phone, UserProfile.Website)}}"}}
Input

UserProfile.Email = "info@test.com"

UserProfile.Phone = ""

UserProfile.Website = "www.test.com"

UserProfile.Email = "info@test.com"

UserProfile.Phone = "+31401234567"

UserProfile.Website = "www.test.com"

Output E: info@test.com
W: www.test.com
E: info@test.com
T: +31401234567
W: www.test.com

 

Related articles

 

 

stringjoin concat
Was this article helpful?
0 out of 0 found this helpful

Comments

0 comments

Article is closed for comments.