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
3 Creeks3 Creeks 

A bug for function containsOnly() ?

Is this the expected behavior of the function containsOnly()?
String foo = 'garbage';		
foo.containsOnly('junk');     //This returns false

String foo = '';		
foo.containsOnly('junk');      //This returns true

system.assertEquals(foo, 'junk');   //This will fail
Seems like a bug but maybe I am missing something.
 
Raja IslamRaja Islam
Documentations says : Returns true if the current String contains characters only from the specified sequence of characters and not any other characters; otherwise, returns false. 

In that case it seems like a bug. Did you try to initialize it with null?
Amit Chaudhary 8Amit Chaudhary 8
You are right It look like some bug. Issue is coming only when string is blank

User-added image

You can try contain function which is working fine when string is blank
 
JyothsnaJyothsna (Salesforce Developers) 
Hi,
containsOnly(inputString) returns true if the current String contains characters only from the specified sequence of characters and not any other characters; otherwise, returns false.

Please try the below code.

String f = 'junk';        

Boolean b1=   f.containsOnly('junk');  

system.assertEquals(true, b1);   //This returns true

String foo = 'abce';        
Boolean b=foo.containsOnly('junk');     //This returns false
system.assertEquals(true, b);


Hope this helps you!
Best Regards,
Jyothsna