0

Facing a weird issue I have spaces and hyphen in my set of strings and now doing at ContainsAll check but it always returns false. If I remove hyphen - between my strings data then it works?

Can someone please explain this behaviour why it doesn't treat same string in different sets differently?

Adding Execute Anonymous code :

Set<String> Broadbands = New Set<String>{'Supplementary–BroadbandNumberRange','Supplementary–BroadbandData','Supplementary–SIMDevice','Supplementary–BroadbandHandset','Broadband','CLP'};
Set<String> a = New Set<String>{'BroadbandNumberRange','BroadbandData','SIMDevice','BroadbandHandset','Broadband','Internet'};
Set<String> bask = New Set<String> {'Supplementary-BroadbandHandset', 'Supplementary-BroadbandNumberRange', 'Supplementary-SIMDevice', 'Broadband'};
Set<String> b= New Set<String> {'BroadbandHandset', 'BroadbandNumberRange', 'SIMDevice', 'Broadband'};

Never works

if(Broadbands.containsall(bask)){
    system.debug('yeah');
}

This works

if(a.containsall(b)){
    system.debug('hello');
}

2 Answers 2

2

For some reason, your editor decided to mix en-dash ("–") with hyphen ("-"). While they look similar, they are definitely not the same and produce different hashes. It's hard to see the difference in a monospace font, but hopefully they should look visually different in this answer (en-dash is significantly longer than hyphen). Always avoid copy-pasting code in to/out of "Office" editors like Microsoft Word, since they may mangle quotes, hyphens, and other punctuation.


Set<String> Broadbands = New Set<String>{'Supplementary-BroadbandNumberRange','Supplementary-BroadbandData','Supplementary-SIMDevice','Supplementary-BroadbandHandset','Broadband','CLP'};
Set<String> a = New Set<String>{'BroadbandNumberRange','BroadbandData','SIMDevice','BroadbandHandset','Broadband','Internet'};
Set<String> bask = New Set<String> {'Supplementary-BroadbandHandset', 'Supplementary-BroadbandNumberRange', 'Supplementary-SIMDevice', 'Broadband'};
Set<String> b= New Set<String> {'BroadbandHandset', 'BroadbandNumberRange', 'SIMDevice', 'Broadband'};
System.assert(Broadbands.containsall(bask),'Hyphens should match correctly');
System.assert(a.containsall(b), 'Non-hyphens should match correctly');
4
  • If I am getting both the sets from SOQL and not manually pasting, Is there a chance that this issue will occur? Shall I remove the hyphen from each element before using them in conditions? Oct 2, 2018 at 20:59
  • @shuklayogesh It all depends on data that you want to operate on. And that how are you populating and comparing the values.
    – Jayant Das
    Oct 2, 2018 at 21:04
  • 1
    @shuklayogesh As long as everything came straight from the database, there shouldn't be any issue. In fact, removing the hyphens might cause issues anyways, since the SOQL filters expect an exact match. You should be just fine in normal situations.
    – sfdcfox
    Oct 2, 2018 at 21:05
  • en-dash, not to be confused with the equally hard to detect with the naked eye: em-dash :-)
    – cropredy
    Oct 3, 2018 at 1:40
1

Just by looking at your code, I can tell the strings in your Broadbands variable and bask are not same.

Broadbands consists of character (en-dash) which you get in quite a few editors e.g., MS Word if you try putting two hyphens and separate words. The bask variable consists only of a - (hyphen).

You will get the desired results if you just copy paste the string values from Broadbands to bask or correct your string values to as desired.

3
  • Not a double-hyphen, an en-dash (unicode: U+2013).
    – sfdcfox
    Oct 2, 2018 at 20:41
  • Yeah that's true, I was taking an example from a common editor as what it turns if you use double hyphens.
    – Jayant Das
    Oct 2, 2018 at 20:42
  • @sfdcfox Thanks for pointing that out, provided the correct reference now.
    – Jayant Das
    Oct 2, 2018 at 20:47

You must log in to answer this question.

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