function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sam KnoxSam Knox 

Use a Wildcard within Contains Function

Hi there - is it possible to use a wildcard within a contains function? I can't seem to find much on the subject other than where wildcards are related to SOQL or search API. I'd like to use it in a class like this:

if (myAtt.Name.contains('Project' + '%' + 'Budget')) {                    
                myAtt.Name = myAcc.Name + '_' + myAtt.Name;               
                }

These are attachments and I'm trying to find string which contain the word 'Project' and 'Budget' but I need to control for cases where the words are separated with just a space, maybe an underscore, or maybe no spaces at all. I've tried the above syntax and it doesn't work for me. 
Best Answer chosen by Sam Knox
Terence_ChiuTerence_Chiu
Have you tried just using an And condition to check if the Name contains both strings ?

if (myAtt.Name.contains('Project') &&  myAtt.Name.contains('Budget') ) {                    
                myAtt.Name = myAcc.Name + '_' + myAtt.Name;               
 }

All Answers

Terence_ChiuTerence_Chiu
Have you tried just using an And condition to check if the Name contains both strings ?

if (myAtt.Name.contains('Project') &&  myAtt.Name.contains('Budget') ) {                    
                myAtt.Name = myAcc.Name + '_' + myAtt.Name;               
 }
This was selected as the best answer
Sam KnoxSam Knox
Thanks! I should have thought of that, but I was stuck on thinking of this as a wildcard scenario.