About this article
This article will explain what the Contains() function is and provide examples of how it can be utilized with the binding syntax in smart templates.
Prerequisites
|
What is the Contains() function?
Contains() is a function that returns True/False depending on a SourceValue containing a SearchValue. Contains() is case sensitive, comparisons between two values that might be using different casings can be achieved by utilizing a Lower() or Upper() functions together with Equals.
Contains() logic
Syntax | {{Contains(SourceValue, SearchValue)}} |
Output | True/False |
Contains() function examples
Example 1
Using the Equals() function to check if a sentence contains the word "random" (case-sensitive).
Binding | {{Contains("This is a longer sentence with some random text", "random")}} |
Output | True |
Binding | {{Contains("This is a longer sentence with some Random text", "random")}} |
Output | False |
Example 2
Using the Contains() function together with Lower() to match the casing used and make a comparison.
Binding | {{Contains(Lower("This is a longer sentence with some random text"), Lower("Random"))}} |
Output | True |
Example 3
Using the Contains() function together with Not() and Lower() to match the casing used and make a comparison.
Binding | {{Not(Contains(Lower("This is a longer sentence with some random text"), Lower("Random")))}} |
Output | False |
Example 4
Using the Contains() function together with IfElse() and Lower() to verify if the input entered in the "Body" question originating from the response form contains the word "legal" (not case-sensitive as Lower() function is used). If the "Body" contains "legal" the output should be "There is legal text in the above document. Ensure it's legally compliant.", otherwise insert an empty input.
Binding | {IfElse(Contains(Lower(Form.Body), "legal"), "There is legal text in the above document. Ensure it's legally compliant.", "")}} | |
Input | Form.Body = "This is legal text." | Form.Body = "This is marketing text." |
Condition |
True |
False |
Output | "There is legal text in the above document. Ensure it's legally compliant." | "" |
Example 5
Using the Contains() function together with IfElse() to display or hide some text based on the value of the "Office.Name" originating from the user profile.
- If the "Office.Name" is "Copenhagen" or "Berlin", the text should be hidden
- If the "Office.Name" is anything else, the text should be shown
Binding | {{IfElse(Contains("Copenhagen, Berlin", UserProfile.Office.Name), VisibilityType.Hidden, VisibilityType.Visible)}} | ||
Input |
UserProfile.Office.Name = "Berlin" |
UserProfile.Office.Name = "Copenhagen" | UserProfile.Office.Name = "Eindhoven" |
Output | Hidden text | Hidden text | Visible text |
Example 6
Using the Contains() function together with IfElse() to see if the "Subject" question originating from the response form matches any of the comparison values (large, cat, small, dog). If the values are "large", "small", "cat", or "dog", the output should be "Match found", otherwise "No match found" should be shown.
Binding | {{IfElse(Contains("large cat, small cat, dog", Form.Subject), "Match found", "No match found")}} | |||||
Input | cat | Cat | large cat | small cat | dog | fox |
Condition | True | False | True | True | True | False |
Output | "Match found" | "No match found" | "Match found" | "Match found" | "Match found" | "No match found" |
Based on the example above, one can also add the Concat() function to see if the "Subject" question originating from the response form matches exactly any of the comparison values (large cat, small cat, dog). If the values are "large cat", "small cat", or "dog", the output is "Match found", otherwise "No match found" is shown.
Binding | {{IfElse(Contains("##large cat##, ##small cat##, ##dog##",Concat("##",Concat(Form.Subject,"##"))), "Match found", "No match found")}} | |||||
Input | cat | Cat | large cat | small cat | dog | fox |
Condition | False | False | True | True | True | False |
Output | "No match found" | "No match found" | "Match found" | "Match found" | "Match found" | "No match found" |
|
Example 7
Using the Contains() function together with IfElse() to see if the "Subject" question originating from the response form matches any of the comparison values (large, cat, small, dog) and adjust the text visibility. If the values are "large", "small", "cat", or "dog", the output should be shown, otherwise, it should be hidden.
Binding | {{IfElse(Contains("large cat, small cat, dog", Form.Subject), VisibilityType.Visible, VisibilityType.Hidden)}} | |||||
Input | cat | Cat | large cat | small cat | dog | fox |
Condition | True | False | True | True | True | False |
Output | Visible | Hidden | Visible | Visible | Visible | Hidden |
Based on the example above, one can also add the Concat() function to see if the "Subject" question originating from the response form matches exactly any of the comparison values (large cat, small cat, dog) and adjust the text visibility. If the values are "large cat", "small cat", or "dog", the output should be shown, otherwise, it should be hidden.
Binding | {{IfElse(Contains("##large cat##, ##small cat##, ##dog##",Concat("##",Concat(Form.Subject,"##"))), VisibilityType.Visible, VisibilityType.Hidden)}} | |||||
Input | cat | Cat | large cat | small cat | dog | fox |
Condition | False | False | True | True | True | False |
Output | Hidden | Hidden | Visible | Visible | Visible | Hidden |
|
Related articles
Comments
0 comments
Article is closed for comments.