Delphi Pos总是返回0 [英] Delphi Pos always returning 0
问题描述
我真的不知道为什么Pos总是返回0而不是char;在字符串
中的位置我必须得到一个php页面的响应,该页面输出一个Content-Type:text / plain
因此,一个示例输出是
2; fulano;。 fulano; 0
3; ciclano; ciclano; 0
4; beltrano; beltrano; 0
5; foo; foo; 0
8; jose; jose; 0
9; maria; maria; 0
,代码为
var
linha,uid,login,senha,email,tipo:WideString;
resposta:TStringList;
I:整数;
开始
尝试
resposta:= TStringList.Create;
resposta.Text:= frmMain.IdHTTP1.Get(frmMain.cdsConfig.FieldByName( Web服务).AsString + listdest.php);
for I:= 0到resposta.Count-1做
开始
linha:= resposta.Strings [i];
如果i = 0,则
Delete(linha,1,1); //如果length(linha)>第一行有一个奇怪的$ FEFF
5然后
开始
uid:=复制(linha,1,Pos(linha,’;’)-1);
Delete(linha,1,Pos(linha,’;’));
login:= Copy(linha,1,Pos(linha,’;’)-1);
Delete(linha,1,Pos(linha,’;’));
senha:= Copy(linha,1,Pos(linha,’;’)-1);
Delete(linha,1,Pos(linha,’;’));
email:= Copy(linha,1,Pos(linha,’;’)-1);
Delete(linha,1,Pos(linha,’;’));
tipo:= Copy(linha,1,Pos(linha,’;’)-1);
Delete(linha,1,Pos(linha,’;’));
结尾;
结尾;
//dlgWait.Close;
除E之外:$ Exception $ b开始
MessageBox(Self.Handle,PWideChar(E.Message),'Erro',MB_OK + MB_ICONERROR + MB_APPLMODAL);
dlgWait.Close;
FreeAndNil(resposta);
结尾;
结尾;
您对 Pos的调用
向后。参数为:
function Pos(const SubStr,Str:_ShortStr; Offset:Integer):Integer;
但是您的代码假定它们是:
function Pos(const Str,SubStr:_ShortStr; Offset:Integer):整数;
所以实际上,它要尝试的是查找 linha
内的';'
,当然,除非 linha
= ' ;'
,它将返回 0
。
另一种表示方式,例如鲁迪说,您的代码不是在大海捞针中寻找针,而是在大海捞针中寻找。
在这些调用的第一个和第二个参数之间交换。
<附带说明,这只是性能提示。而不是每个调用
Pos
,而是保留值的缓存副本... P:= Pos(';',linha);
uid:=复制(linha,1,P-1);
Delete(linha,1,P);
I really don't know why Pos keep returning 0 instead of the char ";" position in string
I have to get a response of a php page which outputs a Content-Type: text/plain
So one example output is
2;fulano;fulano;0
3;ciclano;ciclano;0
4;beltrano;beltrano;0
5;foo;foo;0
8;jose;jose;0
9;maria;maria;0
and the code is
var
linha,uid,login,senha,email,tipo : WideString;
resposta : TStringList;
I : Integer;
begin
try
resposta := TStringList.Create;
resposta.Text := frmMain.IdHTTP1.Get(frmMain.cdsConfig.FieldByName('WebService').AsString+'listdest.php');
for I := 0 to resposta.Count-1 do
begin
linha := resposta.Strings[i];
if i = 0 then
Delete(linha,1,1); // the first line have one wierd $FEFF
if length(linha) > 5 then
begin
uid := Copy(linha,1,Pos(linha,';')-1);
Delete(linha,1,Pos(linha,';'));
login:=Copy(linha,1,Pos(linha,';')-1);
Delete(linha,1,Pos(linha,';'));
senha:=Copy(linha,1,Pos(linha,';')-1);
Delete(linha,1,Pos(linha,';'));
email:=Copy(linha,1,Pos(linha,';')-1);
Delete(linha,1,Pos(linha,';'));
tipo:=Copy(linha,1,Pos(linha,';')-1);
Delete(linha,1,Pos(linha,';'));
end;
end;
//dlgWait.Close;
except on E :Exception do
begin
MessageBox(Self.Handle,PWideChar(E.Message),'Erro',MB_OK+MB_ICONERROR+MB_APPLMODAL);
dlgWait.Close;
FreeAndNil(resposta);
end;
end;
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);
这篇关于Delphi Pos总是返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!