Home > Enterprise >  Delphi: search string with array
Delphi: search string with array

Time:12-04

I need some calculator with Delphi and sometimes my number include '<' and '>'. I need to store them for after usage. For example:

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

This needs to give this output: 320>, but this line fails:

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

I also try this:

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

Here's my code and type.

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;

CodePudding user response:

Well, ContainsText clearly cannot be used, since its second argument is a single string, not an array of strings.

And 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 which does this test for you.

Finally, ContainsText is case-insensitive while IndexStr is not. It is better to compare ContainsText with IndexText/MatchText and ContainsStr with IndexStr/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

Hence, none of these functions tests (1) if a string is a substring of any string in a string array or (2) if any string in a string array is a substring of a given string.

But of course, it is completely trivial to write such a function. Here's for the second case:

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;

In addition, since your string array is actually a character array, you can use TStringHelper.IndexOfAny:

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