Search code examples
delphisetdelphi-10.4-sydney

Check if a string constant is contained in a set of strings


What is the best and most SIMPLE way to do this in modern Delphi (Sydney):

var ThisExtension: string;
if ThisExtension in ['.jpeg', '.jpg', '.jpe', '.jif', '.jfif'] then

The above code gives me this compiler error:

E2015 Operator not applicable to this operand type

Solution

  • The System.StrUtils unit has a simple function for this:

    if System.StrUtils.MatchText(ThisExtension, ['.jpeg', '.jpg', '.jpe', '.jif', '.jfif']) then
    

    I don't know whether this is the most modern approach. (Maybe Generics has a better approach?). But it is fast and simple.