String Contains in Apex
String contains in apex is to check if the given string contains another string. The contains() method is a built in function provided by apex. Following are few usecases when contains method is used in apex.
- 1. Input data validation
The contains() method can help validate user input by ensuring certain characters or patterns are excluded from string fields. For example, it can prevent special characters like @ or # from appearing in the Name field.
- 2. Keyword tracking in records
The contains() method is also useful for monitoring specific keywords within records. For instance, if you need to track feedback containing terms like "disappointed," this method allows you to flag those records for further investigation.
Input string
Salesforce
Search String
force
Result
true
Following is an example of using contains method in apex:
Loading...
In the above example:
- In the above example, two string variables,
string1
andstring2
, are declared with default values. - The
contains()
method checks if string1 contains string2. - The
Contains()
method returns a boolean value indicating whether one string contains the other.
Try It Yourself
Loading...