Skip to main content Die Trailblazer Community steht von 1.2.2025 bis 2.2.2025 nicht zur Verfügung. Bitte planen Sie Ihre Aktivitäten entsprechend.
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
5 Antworten
  1. 14. Jan. 2021, 18:22

    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');

    }

    }

    }

  2. 14. Jan. 2021, 18:17
    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
  3. 14. Jan. 2021, 11:09

    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.');

    }

     
  4. 14. Jan. 2021, 11:05

    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

     
0/9000