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
Guru 91Guru 91 

How to get number of times sub-string appear in a String?

Hi,

I a string and it has 'AND' sting multiple times how to find a number of times sub-string occurs in a string.
He is my code
String str=  AccountName AND AccountPhone;
integer ct=?
Best Answer chosen by Guru 91
FARSANA PSFARSANA PS
Hi,
 Use countMatches() method..

String str= 'AccountName AND AccountPhone';
integer ct=str.countMatches('AND')

Hope this will help...
 

All Answers

FARSANA PSFARSANA PS
Hi,
 Use countMatches() method..

String str= 'AccountName AND AccountPhone';
integer ct=str.countMatches('AND')

Hope this will help...
 
This was selected as the best answer
Raj VakatiRaj Vakati
countMatches(substring) method in String 
Returns the number of times the specified substring occurs in the current String.
 
String str= 'AccountName AND AccountPhone';
integer ct=str.countMatches('AND')