StanE
StanE

Reputation: 2783

Find line break with Pos() in Delphi

I normally work with C++Builder and just started to test in Delphi. I can't find line breaks with Delphi's (XE5) Pos() function and it's weird syntax. What do I wrong? With other functions like StringReplace() it works fine. Here is an example code:

sl := TStringList.Create;
sl.Add('Hello');
sl.Add('world');
sl.Add('!');

if (Pos(sl.Text, #13#10) > 0) then
    ShowMessage('1')
else if (Pos(sl.Text, #13) > 0) then
    ShowMessage('2')
else if (Pos(sl.Text, #10) > 0) then
    ShowMessage('3')
else
    ShowMessage('4');  // Comes always here...

That's how I always did it in C++Builder and had never a problem with it.

Upvotes: 4

Views: 7135

Answers (1)

RRUZ
RRUZ

Reputation: 136431

You are passing the arguments of the Pos function in a wrong order, first you must pass the substring to search and then the buffer string.

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;

Try this

if  Pos(#13#10, sl.Text) > 0 then

or

if Pos(sLineBreak, sl.Text) > 0 then

Upvotes: 8

Related Questions