+ Start a Discussion
sai.sfsai.sf 

String Contains in Set of String

Hi All,

  

     I need to find out the string contains in set of string.Please give me some example.

 

 

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
Saikishore Reddy AengareddySaikishore Reddy Aengareddy

Sai,

 

You can use containsAll method of set only if you are comparing two sets equality.

 

use contains instead

 

if(sub2.Contains(rec.Substring__c))

 

Eg:

set<string> mySet = new Set<String>{'abc', 'efg'};

system.debug('Test>>>> '+mySet.contains('abc'));

All Answers

Saikishore Reddy AengareddySaikishore Reddy Aengareddy

Can you please give an example on what you wanted to achieve?

sai.sfsai.sf

i am trying to find the string from a set of string in apex.

 

 

 like this

 

 

if(rec.Substring__c.containsAll(sub2))

 

 

where sub2 is set<String>

 

but above statement gives an error

Saikishore Reddy AengareddySaikishore Reddy Aengareddy

Sai,

 

You can use containsAll method of set only if you are comparing two sets equality.

 

use contains instead

 

if(sub2.Contains(rec.Substring__c))

 

Eg:

set<string> mySet = new Set<String>{'abc', 'efg'};

system.debug('Test>>>> '+mySet.contains('abc'));

This was selected as the best answer
Starz26Starz26

To do that you will have to loop through the set and use a flag to determine if any value was not matched in the string

 

Boolean noMatch = false;

For(String s : sub2){

 if(!rec.substring__c.contains(s))

    noMatch = true;

}

 

if(noMatch = false){

   //all values matched process here

}

sai.sfsai.sf

Thanks Sam.