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');
}
}
}
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 Arorawww.sachinsf.comHowever 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.');
}
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
Hi Anil,Greetings!You can use || operator to compare two strings in apex suggested in the below link:https://developer.salesforce.com/forums/?id=906F00000008wPRIAYKindly mark it as best answer if it helps so that it can help others in the future.Warm Regards,Shirisha Pathuri
5 Antworten