Best way to find if a string is in a list (without generics)

后端 未结 3 1171
日久生厌
日久生厌 2021-02-01 03:31

I want do something like this:

Result = \'MyString\' in [string1, string2, string3, string4];

This can\'t be used with strings and I don\'t wan

3条回答
  •  清歌不尽
    2021-02-01 04:20

    The code by Burkhard works, but iterates needlessly over the list even if a match is found.

    Better approach:

    function StringInArray(const Value: string; Strings: array of string): Boolean;
    var I: Integer;
    begin
      Result := True;
      for I := Low(Strings) to High(Strings) do
        if Strings[i] = Value then Exit;
      Result := False;
    end;
    

提交回复
热议问题