The EndsWith is a handy Power Apps function that checks if a text string ends with another. It returns a simple true or false, which makes it a natural fit for search boxes and validation rules.
If you've used its sibling, the StartsWith function, this one will feel instantly familiar. It works the same way, just at the other end of the string.
Let's take a look.
Usage
It follows a simple pattern.
- Text: the text string you want to check.
- End Text: the text string you're looking for at the end.
Here's the most basic example.
EndsWith("Hello World","World")
// Returns: true
It's simple, but you should know some things so as not to get some nasty surprises.
Edge Cases
The comparison is case insensitive
EndsWith treats uppercase and lowercase as the same, so a different case still returns true.
EndsWith("Hello World","world")
// Returns: true
Sometimes you would want to consider "World" and "world" as different strings, but this function will treat them as the same. The one exception is the data source. Most sources (collections, Dataverse, SharePoint, and SQL Server) are case insensitive by default, but a few, like Oracle, are not.
Trailing spaces are not ignored
It considers the strings as is, so you should be careful with trailing spaces, which are especially easy to miss at the end of a string.
EndsWith("Hello ","Hello")
// Returns: false
To properly test, you should remove the trailing spaces. To do that you can use the Trim function, or TrimEnds if you only want to touch the ends.
// Use the Trim function to remove the spaces
EndsWith(Trim("Hello "),"Hello")
// Returns: true
It only checks the end
Don't forget that you're only checking if the string ends with another text string. If the text you're looking for is at the start or in the middle, then it always returns false.
EndsWith("Good bye","good")
// Returns: false
Comparing with an empty string returns true
You need to be aware of how it behaves when compared with an empty string. If you're comparing with a variable, this issue will be hidden, but you may have invalid results.
EndsWith("Hello","")
// Returns: true
One could argue that this should not be the case, but Microsoft defined it this way to make it easier to use inside Filter expressions, so you should be aware of it.
Limitations
There are a few things to keep in mind.
Delegation only works with some data sources
This function is only delegable if you're using text strings, and only in Dynamics 365, Common Data Services, and SQL Server. If you don't know what this is, please check my article on delegation and my reference table of the data sources where Filter will be delegable.
When the function isn't delegable for your data source, Power Apps only evaluates the first 500 records (up to 2000 if you raise the limit). On larger data sets you'll get incomplete results with no error.
Localization
Formulas may have localization-based differences. With some regional settings the arguments are separated by a comma, but if you set your regional settings to Portugal, you should use a semicolon instead.
// Comma-based regional settings
EndsWith("Hello World","World")
// Semicolon-based regional settings (for example, Portugal)
EndsWith("Hello World";"World")
Recommendations
Here are some things to keep in mind.
Be careful with case
Since the comparison is case insensitive, be careful when using this function. Sometimes you would want to consider "World" and "world" different strings but, in this function, they will be the same.
Trim your input
If you're using a variable or any other data source to compare, use the Trim function to remove trailing spaces before you check.
Check the type when using variables
Always check the type of the comparison, in case you're using variables; otherwise, you may end up with errors in your Power App.
Don't nest the result
Don't try to nest because you'll get an error in the formula, since EndsWith returns a true/false, and that's not a valid string.
Final Thoughts
The EndsWith function is a simple way to check the end of a text string, but a few quirks can catch you out: the comparison ignores case, trailing spaces matter, and an empty match text always returns true. Keep those in mind and it becomes a reliable little helper in your apps.
Sources
Microsoft's EndsWith and StartsWith function reference
Back to the Power Apps Function Reference.
Photo by Markus Spiske on Unsplash
No comments yet
Be the first to share your thoughts on this article!