Delphi Pos always returning 0

假装没事ソ 提交于 2019-11-29 12:40:10

Your call to Pos is backwards. The parameters are:

function Pos(const SubStr, Str: _ShortStr; Offset: Integer): Integer;

But your code assumes they are:

function Pos(const Str, SubStr: _ShortStr; Offset: Integer): Integer;

So actually what it's trying to do is look for the value of linha within ';', which of course unless linha = ';', it will return 0.

Another way to put it, as Rudy said, instead of looking for a needle in a haystack, your code is looking for a haystack in a needle.

Swap around the first and second parameters to these calls.


On a side note, just a tip for performance. Rather than calling Pos twice for each, keep a cached copy of the value...

P := Pos(';', linha);
uid := Copy(linha,1,P-1);
Delete(linha,1,P);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!