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

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

    You could use AnsiIndexText(const AnsiString AText, const array of string AValues):integer or MatchStr(const AText: string; const AValues: array of string): Boolean; (Both from StrUtils unit)

    Something like:

    Result := (AnsiIndexText('Hi',['Hello','Hi','Foo','Bar']) > -1);
    

    or

    Result := MatchStr('Hi', ['foo', 'Bar']); 
    

    AnsiIndexText returns the 0-offset index of the first string it finds in AValues that matches AText case-insensitively. If the string specified by AText does not have a (possibly case-insensitive) match in AValues, AnsiIndexText returns –1. Comparisons are based on the current system locale.

    MatchStr determines if any of the strings in the array AValues match the string specified by AText using a case sensitive comparison. It returns true if at least one of the strings in the array match, or false if none of the strings match.

    Note AnsiIndexText has case-insensitively and MatchStr is case sensitive so I guess it depends on your use

    EDIT: 2011-09-3: Just found this answer and thought I would add a note that, in Delphi 2010 there is also a MatchText function which is the same as MatchStr but case insenstive. -- Larry

提交回复
热议问题