Thought for 20 seconds
Understanding the Contains() Function in Assette Expressions
Introduction #
In Assette, expressions are powerful tools used within Data Objects to manipulate, compare, and evaluate data for client reports and presentations. The Contains()
function is an essential component in these expressions, enabling developers to determine whether a specific substring exists within a string, with the option to ignore case sensitivity.
This article provides a comprehensive overview of the Contains()
function in Assette, including its purpose, syntax, usage in expressions, practical examples, and best practices for incorporating it effectively into your data processing workflows.
Purpose of Contains()
#
The Contains()
function is used to check whether:
- A string (
mainString
) contains a specific substring (subString
).
It returns a boolean value:
True
: If the substring is found within the main string.False
: If the substring is not found.
An optional ignoreCase
parameter allows you to specify whether the search should be case-sensitive.
This function is crucial for:
- Conditional Logic: Making decisions based on the presence of certain values.
- Data Validation: Ensuring data meets specific criteria.
- Filtering Data: Selecting records that match certain conditions.
Syntax #
mainString
: The string to search within.subString
: The substring to search for.ignoreCase
: (Optional) A boolean value indicating whether to ignore case sensitivity. If set totrue
, the search is case-insensitive. If omitted or set tofalse
, the search is case-sensitive.
Contains(mainString, subString, ignoreCase: true)
Usage in Expressions #
Case-Sensitive Search #
If ignoreCase
is omitted or set to false
, the Contains()
function performs a case-sensitive search.
- Returns
True
only ifdocumentTitle
contains “Quarterly Report” with the exact casing. - Returns
False
if the casing differs.
Contains(documentTitle, "Quarterly Report")
Case-Insensitive Search #
By setting ignoreCase
to true
, the Contains()
function will perform a case-insensitive search.
- Returns
True
ifdocumentTitle
contains “Quarterly Report”, “quarterly report”, “QUARTERLY REPORT”, etc. - Ignores case differences between
mainString
andsubString
.
Contains(documentTitle, "quarterly report", ignoreCase: true)
Practical Examples #
Filtering Transactions #
Objective: Identify if a transaction is of a specific type, considering case sensitivity.
Explanation:
- Performs a case-sensitive search for “Refund” in
transactionType
. - Use this when the exact casing is important.
Contains(transactionType, "Refund")
Best Practices #
- Specify
ignoreCase
When Necessary- Be explicit about case sensitivity by setting
ignoreCase
totrue
orfalse
.
- Be explicit about case sensitivity by setting
- Understand Default Behavior
- If
ignoreCase
is optional and omitted, know that the default is case-sensitive. Always specifyignoreCase
if case insensitivity is required.
- If
- Trim Whitespace
- Remove unnecessary spaces using
Trim()
.
- Remove unnecessary spaces using
- Avoid False Positives
- Be precise with
subString
to prevent unintended matches.
- Be precise with
Common Mistakes to Avoid #
- Ignoring Case Sensitivity
- Not specifying
ignoreCase
can lead to missed matches or false positives.
- Not specifying
- Assuming Default Case Sensitivity
- If unsure about the default behavior, always specify the
ignoreCase
parameter.
- If unsure about the default behavior, always specify the
- Not Handling Null Values
- Attempting to use
Contains()
on anull
mainString
may cause errors.
- Attempting to use
- Assuming Exact Matches
- Remember that
Contains()
searches for any occurrence ofsubString
withinmainString
.
- Remember that