I have this code:
var
ExtString: string;
const
Extensions : array[0..4] of string = ('.rar', '.zip', '.doc', '.jpg', '.gif');
if ExtString in Extensions then
On the last line, I get an error:
[DCC Error] E2015 Operator ('then') not applicable to this operand type
I think I can not do this, so how can I properly perform my task?
The includes() method returns true if an array contains a specified value. The includes() method returns false if the value is not found.
contains() method in Java is used to check whether or not a list contains a specific element. To check if an element is in an array, we first need to convert the array into an ArrayList using the asList() method and then apply the same contains() method to it.
Array. indexOf() Takes any value as an argument and then returns the first index at which a given element can be found in the array, or -1 if it is not present.
As you have found you can't check for a String in an Array of String, using in
.
You could use this function instead of the if
statement.
function StrInArray(const Value : String;const ArrayOfString : Array of String) : Boolean;
var
Loop : String;
begin
for Loop in ArrayOfString do
begin
if Value = Loop then
begin
Exit(true);
end;
end;
result := false;
end;
You can call it like this.
if StrInArray(ExtString,Extensions) then
The StrUtils.pas
has this already defined.
function MatchStr(const AText: string; const AValues: array of string): Boolean;
Initialise a TStringList instance from the constant array and use IndexOf().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With