Here is some code to illustrate the problem.
String str1 = ''; System.debug(str1.containsOnly('W'));
I would expect
false as the result but surprisingly the result was
true. I don't understand why this is the case considering an empty string does not contain a
'W' character. Is this expected behavior or a bug?
Edit: After the responses I have received so far, I think I need to rephrase my question and make it a more fundamental one. By so doing, the original question will answered.
One underlying assumption I am making is that the documentation of the method preceded and was the basis of the implementation of the method and not vice versa. If you think this assumption is wrong, please let me know.
Thus putting aside how
containsOnly() is currently implemented, how should the documentation
of the method be understood? It is more of a question of meaning or semantics than a technical one. This is the given description.
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.
Here are the possible interpretations so far:
The current string does not contain any characters outside of the characters in the input string. (@PhilW)
The current string contains any of the characters in the input string (
containsAny(inputString)) && interpretation 1. (My understanding).
Please let me know how you understand the documentation or the phrase containsOnly and if you have any other plausible interpretations, I will add them to the list. I think some consensus is needed on this before getting into the actual implementation of the method.