function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Bramha1Bramha1 

Compare strings or Integers!!!!!

Hi,

 

     I know how to compare strings and Integers, but i am looking for something like below:

  I have 3 strings :

 

 String str1 =  ' abcd' ;

String  str2 = ' cded' ;

String str3 = ' ==' ; ( This is a oparator and can be any != OR > OR < OR any operator) 

 

Now i need a method or any way to check the above three so that i get a boolean value back based on these three arguments.

 

 

Thanks

Bramha

 

 

Best Answer chosen by Admin (Salesforce Developers) 
soofsoof

Does this work for you?

 

public Boolean compare(String str1, String str2, String op) {
	return
		(op == '==' && str1 == str2) || (op == '!=' && str1 != str2) ||
		(op == '>' && str1 > str2) || (op == '<' && str1 < str2);
}

 

Thanks.

All Answers

soofsoof

Does this work for you?

 

public Boolean compare(String str1, String str2, String op) {
	return
		(op == '==' && str1 == str2) || (op == '!=' && str1 != str2) ||
		(op == '>' && str1 > str2) || (op == '<' && str1 < str2);
}

 

Thanks.

This was selected as the best answer
Nisse Knudsen.ax1026Nisse Knudsen.ax1026

soof wrote:

Does this work for you?

 

public Boolean compare(String str1, String str2, String op) {
	return
		(op == '==' && str1 == str2) || (op == '!=' && str1 != str2) ||
		(op == '>' && str1 > str2) || (op == '<' && str1 < str2);
}

 

Thanks.


Great Snippet! I would have done sth with IF/ELSE, but this is really good! thanks for sharing!