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
Phani PYDIMARRY 11Phani PYDIMARRY 11 

Unable to compare two lists with string values

Hello,

I am trying to compare two strings which have a Semi coloumn. I have used split but I am unable to compare the strings. In the below example, the debug gives an error for BALL but other strings pass. Can anyone help me what I am I missing here?
 
STRING TEST1 = 'MY ; BALL ; CAR; PEN';
LIST<STRING> TEST2 = TEST1.SPLIT(';');
SYSTEM.debug(TEST2);

STRING TEST3 = 'MY ; BALL; CAR';
for(string test4:TEST3.split(';')){
    system.debug(test4);
    if(TEST2.contains(test4)){
        system.debug('working');
        system.debug(test2);
    }
    else{
        system.debug('not working');
    }
}

 
Best Answer chosen by Phani PYDIMARRY 11
Phani PYDIMARRY 11Phani PYDIMARRY 11
Thanks Devi,

With the help of your suggestion, I have used deleteWhitespace() to remove any whitespace in the strings before I compare. Thanks for your help. 

All Answers

Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi Phani,
Your code is correct but test1 string has space after BALL.Just remove space.Try by removing that space in test1.
STRING TEST1 = 'MY ; BALL; CAR; PEN';
LIST<STRING> TEST2 = TEST1.SPLIT(';');
SYSTEM.debug(TEST2);

STRING TEST3 = 'MY ; BALL; CAR';
for(string test4:TEST3.split(';')){
    system.debug(test4);
    if(TEST2.contains(test4)){
        system.debug('working');
        system.debug(test2);
    }
    else{
        system.debug('not working');
    }
}

Hope this helps you
Let me know if this helps you. Kindly mark it as solved so that it may help others in future.

Thanks and Regards
Phani PYDIMARRY 11Phani PYDIMARRY 11
Hi Devi,

Thanks for the reply. This is just a test class I am trying to implement for a bigger implementation where Users manually enter into TEST3 and there is TEST1 is a Multi Picklist value. How to eliminate spacing and then compare?
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Use trim() method on each string before comparing strings.Trim() removes spaces from string.

Please refer below link which might help you in this
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_string.htm

Hope this helps you
Let me know if this helps you. Kindly mark it as solved so that it may help others in future.

Thanks and Regards
Phani PYDIMARRY 11Phani PYDIMARRY 11
Thanks Devi,

With the help of your suggestion, I have used deleteWhitespace() to remove any whitespace in the strings before I compare. Thanks for your help. 
This was selected as the best answer
Ankit Solanki 6Ankit Solanki 6
STRING TEST1 = 'MY ; BALL ; CAR; PEN';
LIST<STRING> TEST2 = TEST1.SPLIT(';');
SYSTEM.debug(TEST2);
STRING TEST3 = 'MY ; BALL; CAR';
for(string test4:TEST3.split(';')){
system.debug(test4);
if(TEST2.contains(test4)){
system.debug('working');
system.debug(test2);
}
else{
system.debug('not working');
}
}
If you want to compare whole string then code like this:
String test1 = 'MY;BALL;CAR;PEN';
String test2 = 'MY;BALL;CAR';
if( test1.equals(test2)){
system.debug('true');
}
else{
system.debug('false');
}
If you want to compare words one by one then code like this:
String test1 = 'MY;BALL;CAR;PEN';
String test2 = 'MY;BALL;CAR';
for(String test1demo : test1.split(';')){
for(String test2demo : test2.split(';')){
if(test1demo.contains(test2demo)){
system.debug('match');
}
else{
system.debug('Not Match');
}
}
}