You need to sign in to do that
Don't have an account?
Phani 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?
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'); } }
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
Your code is correct but test1 string has space after BALL.Just remove space.Try by removing that space in test1.
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
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?
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
With the help of your suggestion, I have used deleteWhitespace() to remove any whitespace in the strings before I compare. Thanks for your help.
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');
}
}
}