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
Sanjat Samal 8Sanjat Samal 8 

How can I compare a string with a set ?

I Need to compare in IF condition but the one is "String" and another is a "Set". Please suggest
Best Answer chosen by Sanjat Samal 8
Raj VakatiRaj Vakati
String s ='Hello';

Set<String> st = new Set<String>();
st.add('world');
st.add('Hello')

if(st.conatins(s)){
System.debug('String is contains in Set');
}else{
System.debug('NOOO');
}

 

All Answers

Sitarama MurthySitarama Murthy
Hi Sanjat,

Loop over the Set of values and inside for loop compare your String value.

Example:
Set<ID> setIds = New Set<ID>();
String str = 'value';
for(ID ids : setIds){
 if(ids == str){
// your logic
}
}


Thanks,
Ram
Raj VakatiRaj Vakati
String s ='Hello';

Set<String> st = new Set<String>();
st.add('world');
st.add('Hello')

if(st.conatins(s)){
System.debug('String is contains in Set');
}else{
System.debug('NOOO');
}

 
This was selected as the best answer