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

后端 未结 3 1170
日久生厌
日久生厌 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

    Here is a function that does the job:

    function StringInArray(Value: string; Strings: array of string): Boolean;
    var I: Integer;
    begin
      Result := False;
      for I := Low(Strings) to High(Strings) do
      Result := Result or (Value = Strings[I]);
    end;
    

    In fact, you do compare MyString with each string in Strings. As soon as you find one matching you can exit the for loop.

提交回复
热议问题