+ Start a Discussion
yarramyarram 

How to compare two string Arrays..............Urgent

Hi All,

 


i have two string values like Blockno[ (1,2,3) ] and BlockNo1[ (1,3,4) ]. while comparing these two strings '3' is common value , so when matching the value it should display error message.

 

below mentioned code is not working for me .....

 

So , i want to compare Two string Arrays...How can i achive this ?

 

 

        for(SpecimenTest__c boxes : testlist)
        {
         
                String blkno=boxes.BlockNo__c;//here the value like[ (1,2,3) ]
                String blkno1=speci.BlockNo__c; //here the value like[ (1,3,4) ]               
                String[] st = blkno.split(' , ');
                String[] st1 = blkno1.split(' , ');
                for(integer i=1;i<=st.size();i++)
                {
                    for(integer j=1;j<=st1.size();j++)
                    {
                    Integer resultb =st1[0].compareTo(st[0]);
                     
                        if(resultb ==0)
                        {
                            error code;
                        }
                    }
                }

        }

 

Please help me ...

Thanks,

Yarram

kibitzerkibitzer

Well, the obvious answer is that you probably want your comparison to be:

    Integer resultb =st1[i].compareTo(st[j]);

 

It's pretty inefficient code.

 

You can improve it as follows (approximate code - not tested):

 

String[] st = blkno.split(' , ');
Set<String> st1 = new Set<String>(blkno1.split(' , '));

for(String s: st) if (st1.contains(s)) { error code processing }

 

Dan

 

yarramyarram

Hi kibitzer,

 

Thanks for replying,

 

its not working.


i want to check each and every value of two strings. if any one
value  maching it should  raise a error.

 

Thanks,

Rakesh.

Santosh KumbarSantosh Kumbar

Hey Try below code :

String blkno='[(1,2,3)]';
String blkno1='[(1,3,4)]'; 
String[] st = blkno.split(',');   // Split had issue i suppose : You added '<space>,<space>' . i think it should be ',' , No space
String[] st1 = blkno1.split(',');
for(integer i=1;i<=st.size();i++)
{
	for(integer j=1;j<=st1.size();j++)
	{
		Integer resultb =st1[0].compareTo(st[0]); //It was [0]th element always always
		system.debug('st1[0] = '+st1[0]+'---st[0]'+st[0]);
		if(resultb ==0)
		{
			system.debug('Enterd Error part');
		}
	}
}

 Regards

Santosh

www.santoshkumbar.com

kibitzerkibitzer

"It's not working" doesn't provide much information.

 

Conceptually, the code does exactly what you're looking for - it compares each element in one string with all of the elements in the other and flags an error if any value matches.

 

But I didn't check the exact syntax - if it isn't working, use some debugging statements to make sure the the strings are being split correctly.

 

Dan

 

Gunners_23Gunners_23

Hi,

 

kibitzer is right, it will work fine and the first one was grossly inefficient. Probably because of some syntax issues it didn't

 

generate appropriate results. Pls find the code below

 

 

String a1 = '1,2,3';
String a2 = '3,4,6';

Set<String> blkno =new Set<String>(a1.split(','));
Set<String> blkno1=new Set<String>(a2.split(','));

System.debug(blkno);
System.debug(blkno1);
for(String s : blkno)
{
if (blkno1.contains(s))
{
    System.debug('ERROR');
}
}
yarramyarram

Thanks for All,

 

Gunners code is working one way like.....

String a1 = '(2,4)';
String a2 = '(2,6)'; here it throws the error.

 

But, when i enter one value like

 

String a1 = '(2,4)';
String a2 = '(2)';      not working this scenario....

 

 here first i was inserted T-0833  with (2,4) valule. second time try to insert test T-0834  with (2) value its inserted without

error. here '2' is duplicate. so,it should not inserting the record T-0834.

How can i restrict the insertion.

 

String a1 = '(2,4)';
String a2 = '(2)';

Set<String> blkno =new Set<String>(a1.split(','));
Set<String> blkno1=new Set<String>(a2.split(','));

System.debug(blkno);
System.debug(blkno1);
for(String s : blkno)
{
if (blkno1.contains(s))
{
    System.debug('ERROR');
}
}

 


How can i achive this .....

 

Thanks

Yarram.

Gunners_23Gunners_23

Its because in the

 

String a1 = '(2,4)';-----> results in set containing two values (2,4)

 

String a2 = '(2)'; -------> results in set containing 1 value (2)

 

Hence (2) is not equal (2

 

So to tackle this you can use replace method to ignore braces and then put into Set

 

String a1 = '(1,2,3)';
String a2 = '(2)';
a1 = a1.replace('(','');
a1 = a1.replace(')','');
a2 = a2.replace('(','');
a2 = a2.replace(')','');
System.debug(a1);
System.debug(a2);
Set<String> blkno =new Set<String>(a1.split(','));
Set<String> blkno1=new Set<String>(a2.split(','));

System.debug(blkno);
System.debug(blkno1);
for(String s : blkno)
{
if (blkno1.contains(s))
{
    System.debug('ERROR');
}
}

 

yarramyarram

Thanks for replying,

 

"replace" is not working.

 

How can i trim/remove the "(" and ")" from the string.

 

please help me .

 

Thanks,

Yarram.

Gunners_23Gunners_23

You sure its not working?....I just tried in my Dev Console and its working fine..

 

Did you copy the exact code?

yarramyarram

yes, replace method is working now. but when i given single digit value its not checking. duplicate record was inserted.

 

a1='1,3,5';

a2='3';-----this one is inserted without checking.


plz, can u tell me why its not checking the single value.

 

Thanks,

Yarram.

Gunners_23Gunners_23

You need to tell what kind of inputs you'll be giving...In your first example it was about braces so i replaced the braces with '

 

'(nothing).  If you want to insert any special character then before comparing you need to replace it with the same. Same case

 

with "Single Quotes" (') in your example.

yarramyarram

sorry, i am not using single quotes.... i did like this ....bellow mentioned are the degbug log data...can you check bellow mentioned records  are cintains the duplicate value is "5" in block no  column . how can we restrict the duplicate records.

 

01:34:29.161 (161739000)|SYSTEM_METHOD_ENTRY|[2594]|System.debug(ANY)
01:34:29.161 (161751000)|USER_DEBUG|[2594]|DEBUG|RTRTRTRTRTRTRTRTRTRT{ 3,  5, 1}
01:34:29.161 (161760000)|SYSTEM_METHOD_EXIT|[2594]|System.debug(ANY)
01:34:29.161 (161772000)|SYSTEM_METHOD_ENTRY|[2595]|System.debug(ANY)
01:34:29.161 (161798000)|USER_DEBUG|[2595]|DEBUG|{ 6, 5}
01:34:29.161 (161808000)|SYSTEM_METHOD_EXIT|[2595]|System.debug(ANY)

 

 

 

Thanks,

Yarram.