+ Start a Discussion
Arjun SrivastavaArjun Srivastava 

Compare 2 string arrays

Hi,

I have 2 strings contating words separated by comma.
I want to compare the two strings.

Example:
String1={sfdc,developer,star,Apex};

String2={software Developer,Java,cloud,apex};

can anyone hepl me doing this.

 

Thanks 

Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,

 

Try the below code snippet as reference:

 

String1={sfdc,developer,star,Apex};

String2={software Developer,Java,cloud,apex};

 

String[] st = String1.split(' , ');

 

 

for(integer i = 0; i<st.size(); i++)

{

if(String2.contains(string.valueof(st[i])))

{

 

}

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

All Answers

Navatar_DbSupNavatar_DbSup

Hi,

 

Try the below code snippet as reference:

 

String1={sfdc,developer,star,Apex};

String2={software Developer,Java,cloud,apex};

 

String[] st = String1.split(' , ');

 

 

for(integer i = 0; i<st.size(); i++)

{

if(String2.contains(string.valueof(st[i])))

{

 

}

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

This was selected as the best answer
Anup JadhavAnup Jadhav

Hi sfdc_star,

 

Can you provide more information about the nature of comparision? 

 

Are the 2 strings equal, if they have the same list of comma seperated values but in a different order? If yes, then the following code should work:

 

// returns 1 if both string have the same list of comma seperated values (order does not matter)

// returns 0 if they don't have same list of values

 

private Boolean compareCommaSeperatedStrings(String val1, String val2) {

 

if(val1 == null || val2 == null) {

return false;

}

 

List<String> val1List  = val1.split(',');

List<String> val2List = val2.split(',');

Set<String> val1Set = new Set<String>();

Set<String> val2Set = new Set<String>();

 

for(String s : val1List) {

val1Set.put(s);

}

 

       for(String s : val2List) {

             val2Set.put(s);

       }

       

      if(val1Set.size() > val2Set.size()) {

return val1Set.containsAll(val2Set);

      }

      

      if(val1Set.size() > val2Set.size()) {

              return val2Set.containsAll(val1Set);

      }

      

      return val1Set.containsAll(val2Set);

     

}

 

The code is self explanatory.

 

Hope this helps!

harsha__charsha__c

You can make it simpler as this..........

 

 

 

public with sharing class test1
{
    set<String> s1 = new set<String>{'harsha','vardhan','sandeep','sankhla','mohammad','naved'};
    set<String> s2 = new set<String>{'vardhan','sandeep','sankhla','mohammad','naved' };
    public void compareSets()
    {
        if(s1.containsAll(s2) && s2.containsAll(s1))
        {
            system.debug('Both are equal');
        }
        else
        {
            system.debug('Both the sets are not equal');
        }
    }
}

 

 

If this makes your problem solved then  mark it as a Solution