+ Start a Discussion
anil Kumaranil Kumar 

how to compare two string values in apex

Hi All,

I have to compare two string vales in apex whether they are same vales or not. Could anyone please help me.  

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

Thanks,
Anil Kumar
Best Answer chosen by anil Kumar
Malika Pathak 9Malika Pathak 9


Hi anil Kumar,

Please find the solution.

public static void CompareString(){
        String str1 = 'Pay TV;AVOD;Basic TV;';
        String str2 = 'Basic TV;Pay TV;AVOD;';
        
        List<String> str1List = str1.split(';');
        List<String> str2List = str2.split(';');
        for(Integer i=0;i<str1List.size();i++){
            if(str2List.contains(str1List[i])){
                system.debug('Yes this word '+str1List[i]+' is contains in str2List');
            }
        }
        
    }

All Answers

ShirishaShirisha (Salesforce Developers) 
Hi Anil,

Greetings!

You can use || operator to compare two strings in apex suggested in the below link:

https://developer.salesforce.com/forums/?id=906F00000008wPRIAY

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
Daniel AhlDaniel Ahl
Hello, if you want to compare the two strings as is, you can just do like this: 
String str1 = 'Pay TV;AVOD;Basic TV';
String str2 = 'Basic TV;Pay TV;AVOD';

system.debug(str1 == str2); // false


 
Daniel AhlDaniel Ahl
However if you would like to compare each values in the string to see if they are the same, you could do something like this:
String str1 = 'Pay TV;AVOD;Basic TV';
String str2 = 'Basic TV;Pay TV;AVOD';

List<String> str1List = str1.split(';');
List<String> str2List = str2.split(';');
Boolean valuesAreTheSame = true;
for(String tmp1 : str1List){
    if(!str2List.contains(tmp1)){
       valuesAreTheSame = false; 
    }
}

if(valuesAreTheSame){
    system.debug('Values in the two strings are the same.');
} else {
    system.debug('Values in the two strings are not the same.');
}

 
sachinarorasfsachinarorasf
HI Anil Kumar,
Try using the String.equals() method within Apex. It does a string comparison but it is case-sensitive.
String myString1 = 'abcd';
String myString2 = 'abcd';
Boolean result =  myString1.equals(myString2);
System.debug(result);
Try using the String.equalsIgnoreCase() method within Apex. It does a string comparison, it is not case-sensitive.
String myString1 = 'abCD';
String myString2 = 'abcd';
Boolean result =  myString1.equalsIgnoreCase(myString2);
System.debug(result);
Thanks and Regards,
Sachin Arora
www.sachinsf.com
Malika Pathak 9Malika Pathak 9


Hi anil Kumar,

Please find the solution.

public static void CompareString(){
        String str1 = 'Pay TV;AVOD;Basic TV;';
        String str2 = 'Basic TV;Pay TV;AVOD;';
        
        List<String> str1List = str1.split(';');
        List<String> str2List = str2.split(';');
        for(Integer i=0;i<str1List.size();i++){
            if(str2List.contains(str1List[i])){
                system.debug('Yes this word '+str1List[i]+' is contains in str2List');
            }
        }
        
    }
This was selected as the best answer