0

I have 2 string variables. One of them holds data retrieved from custom metadata and another is explicitly declared. Both return exact same data when debugged, but still if I compare them, it returns false.

What can be the possible reason? Also please suggest how I can make them the same, as I have a bigger business logic involving this.

Thank you.

PS_Set__c field is of type Long Text Area.

Data stored in PS_Set__c in custom metadata is exactly in this format: Test A, Test B, Test C

List<Custom_Test__mdt> p = [SELECT PS_Set__c from Custom_Test__mdt];

List<String> split = p[0].PS_Set__c.split(',');

string keyword = 'Test C';

system.debug(split[2]==keyword);                // returning false
system.debug(string.valueof(split[2])==keyword);    // returning false

2 Answers 2

0

It seems there is space in the string stored in custom metadata. Either remove the space or use trim to remove the space from the string

e.g. system.debug(split[2].trim()==keyword); 
1
  • Thank brother, lifesaver. Mar 9, 2023 at 8:07
-2

for comparing string in Apex, you should always use the String class and its method [e.g. firstString.equals(secondString)]. it will give you a correct comparision result by checking data type as well.

refer this link: Apex String Class

1
  • eh, -1 from me because this information is incomplete and a bit incorrect. String's equals() has effectively the same type checks as == does. Matching types doesn't guarantee that you'll get the result that you think you should be getting. If this were an issue of types, you would almost certainly be getting an error trying to save the code in the first place. It's also a case-sensitive equality check (unlike == which is case-insensitive). It's also longer to type. Unless you're using the string method for the sake of being consistent in your codebase, == is probably preferred.
    – Derek F
    Mar 9, 2023 at 11:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .