-1

I cannot find out why this happen. On windows I have a txt file from which I read first line with text '[směr:A->B]'.

I thet my code with debugger. I use breakpoints so I see, that the result of AnsiPos(line, '[sm') is 0. Why? I suppost to get 1. But I tried to search for AnsiPos(line, 'sm') or just AnsiPos(line, 'm') and still not positive result. Why? I tried Pos() too. Should I use different function? What I missed?

readln(f,line);
if line[1]='[' then
begin;
  if line[2]='s' then
     if AnsiPos(line, '[sm') > 0 then
        begin
          if AnsiPos(line, 'B->A') > 0 then
            smerTestu := 'B'
          else
            smerTestu := 'A';
        end;
end;
John Boe
  • 3,501
  • 10
  • 37
  • 71

1 Answers1

1

From System.Pos, it is stated that the function:

function Pos(const SubStr, Str: _ShortStr; Offset: Integer): Integer;
function Pos(const SubStr, Str: UnicodeString; Offset: Integer): Integer; overload;
function Pos(const SubStr, Str: _WideStr; Offset: Integer): Integer; overload;
function Pos(const SubStr, Str: _RawByteStr; Offset: Integer): Integer;

is defined as:

Locates a substring in a given string.

The Pos method returns the index of the first occurence of Substr in Str, starting the search at Offset.

This method returns zero if Substr is not found or Offset is invalid (for example, if Offset exceeds the String length or is less than 1).

The Offset argument is optional. Offset is set to 1 by default, if no value for Offset is specified it takes the default value to start the search from the beginning.


In your code, you have mixed up the order of parameters Str and SubStr.

The AnsiPos() function works the same way.

LU RD
  • 34,438
  • 5
  • 88
  • 296