Delphi: 用数组搜索字符串

Delphi: search string with array

我需要一些带有 Delphi 的计算器,有时我的数字包括 '<''>'。我需要存储它们以备后用。例如:

fnc_ProcessNumber('0.320>','100',multiplication)

这需要给出这样的输出:320>,但是这一行失败了:

boolControl := ContainsText(strEnteredResult,[lessThan,greaterThan]);

我也试试这个:

boolControl := IndexStr(strEnteredResult,[lessThan,greaterThan]);

这是我的代码和类型。

type TMaths  =  (addition,subtraction,multiplication,division);

function fnc_ProcessNumber(strEnteredResult,strProcessedNumber:string;Math:TMaths) : string;
const lessThan : string = '<';
const greaterThan : string = '>';
var
  intIndexCounter,intIndexMath:Integer;
  extNewResult:Extended;
  boolControl,boolLess,boolGreater:Boolean;
begin
  Result := 'null';
  boolControl := ContainsText(strEnteredResult,[lessThan,greaterThan]);
  if boolControl then
  begin
    case intIndexCounter of
      0:
        begin
          intIndexMath := AnsiPos(lessThan,strEnteredResult);
          boolLess := True;
        end;
      1:
        begin
          intIndexMath := AnsiPos(greaterThan,strEnteredResult);
          boolGreater := True;
        end;
    end;
    strProcessedNumber := Copy(strEnteredResult,intIndexMath,1);
  end
  else
  begin
    extNewResult := StrToFloat(strEnteredResult);
  end;
  case Math of
    addition:
      begin
        extNewResult := extNewResult + StrToFloat(strProcessedNumber);
      end;
    subtraction:
      begin
        extNewResult := Abs(extNewResult - StrToFloat(strProcessedNumber));
      end;
    multiplication:
      begin
        extNewResult := extNewResult * StrToFloat(strProcessedNumber);
      end;
    division:
      begin
        extNewResult := extNewResult / StrToFloat(strProcessedNumber);
      end;
  end;
  Result       := FloatToStr(extNewResult);
  if boolLess then Result := lessThan+Result;
  else if boolGreater then Result := Result+greaterThan;
end;

嗯,ContainsText 显然不能使用,因为它的第二个参数是单个字符串,而不是字符串数组。

还有 IndexStr cannot be used either, since it merely tests whether the text is (exactly) one of the elements of the array; it doesn't do any substring testing. In addition, it returns an integer, the index of the match or -1, so you would need to compare the result against <> -1 to get a boolean. Or, you can use MatchStr 为您做这个测试。

最后,ContainsText 不区分大小写,而 IndexStr 则不区分大小写。最好将 ContainsTextIndexText/MatchTextContainsStrIndexStr/MatchStr 进行比较:

Case sensitive Case insensitive
Substring found in string? ContainsStr ContainsText
Index of string in string array. IndexStr IndexText
String found in string array? (Index <> -1) MatchStr MatchText

因此,none 这些函数测试 (1) 字符串是否是字符串数组中任何字符串的子字符串或 (2) 字符串数组中的任何字符串是否是给定字符串的子字符串.

当然,编写这样的函数是完全微不足道的。这是第二种情况:

function ContainsAnyStr(const AText: string; const AStrings: array of string): Boolean;
begin
  for var i := 0 to High(AStrings) do
    if ContainsStr(AText, AStrings[i]) then
      Exit(True);
  Result := False;
end;

function ContainsAnyText(const AText: string; const AStrings: array of string): Boolean;
begin
  for var i := 0 to High(AStrings) do
    if ContainsText(AText, AStrings[i]) then
      Exit(True);
  Result := False;
end;

另外,由于你的字符串数组实际上是一个字符数组,你可以使用TStringHelper.IndexOfAny:

'123>'.IndexOfAny(['<', '>']) <> -1