-2

I have to compare two string values in apex to determine whether they contain the same semicolon separated values or not (in any order). Could anyone please help me.

String str1 = 'Pay TV;AVOD;Basic TV';
String str2 = 'Basic TV;Pay TV;AVOD';

Thanks, Anil Kumar

1
  • Please take a look at How to Ask and help center. Every question should demonstrate your work and research effort towards solving a question. SFSE's model is not to provide code for requirements. – David Reed Jan 15 at 18:28
1

If you want to check if a string contains the same values as the other string but they aren't in the same order, you can load them to two sets and compare between them.

String str1 = 'Pay TV;AVOD;Basic TV'; 
String str2 = 'Basic TV;Pay TV;AVOD';
List<String> str1List = str1.split(';');
List<String> str2List = str2.split(';');
Set<String> str1Set = new Set<String>();
Set<String> str2Set = new Set<String>();
str1Set.addAll(str1List);
str2Set.addAll(str2List);

if(str1Set.equals(str2Set))
    system.debug('true'); // result: true
else
    system.debug('false'); 
1
  • str1set == str2set would work just as well. – sfdcfox Jan 15 at 20:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.