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
Newbie_edmNewbie_edm 

How to Check sub string in the string?

Hi guys,

I need you guys help here, I have string which contains [A], I need to call class only if that main string has that [A] format it should not call class if it finds just A in that main string. How can we achieve this in Apex code?

Thanks.
Best Answer chosen by Newbie_edm
Pankaj_GanwaniPankaj_Ganwani
Hi Venkat,

You can use indexOf(substring) method of string. Like : 

String str = 'abc[A]';
if(str.indexOf('[A]')!=-1)
{
       //execute code
}

You can find more documentation here:

https://developer.salesforce.com/forums/ForumsMain?state=id#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Developer_Forums&criteria=ALLQUESTIONS&id=906F0000000BZtjIAG

All Answers

Pankaj_GanwaniPankaj_Ganwani
Hi Venkat,

You can use indexOf(substring) method of string. Like : 

String str = 'abc[A]';
if(str.indexOf('[A]')!=-1)
{
       //execute code
}

You can find more documentation here:

https://developer.salesforce.com/forums/ForumsMain?state=id#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Developer_Forums&criteria=ALLQUESTIONS&id=906F0000000BZtjIAG
This was selected as the best answer
Pankaj_GanwaniPankaj_Ganwani
Please close this thread by marking the best answer if this solves your issue.
Dario BakDario Bak
Actually is way simpler. Apex strings do have a contains method. Example below:
 
Boolean result=myString.contains('XAPPIA');
Newbie_edmNewbie_edm
Thanks lot Pankaj. It worked.