function Fenli(Src: string; Before, After: string): string;//字符串分离函数
var
Pos1, Pos2: Word;
Temp: string;
begin //使用方法:Fenli('www.zhuanhou.cn','www.','.cn') 得到的结果为:zhuanhou
Temp := Src;
Pos1 := Pos(Before, Temp);
delete(Temp, 1, Pos1+length(Before));
Pos2 := Pos(After, Temp);
if (Pos1 = 0) or (Pos2 = 0) then
begin
result := '';
Exit;
end;
Pos1 := Pos1 + Length(Before);
result := Copy(Src, Pos1, Pos2);
end;请问这个函数为什么在删除的时候还要加上length(Before). Pos1 不是已经包含了完整的长度了吗? 我用我的代码测试了,是可以,但是这个也可以,我真的看不懂。有本身的长度,还要加上Before 参数的长度。
var
Pos1, Pos2: Word;
Temp: string;
begin //使用方法:Fenli('www.zhuanhou.cn','www.','.cn') 得到的结果为:zhuanhou
Temp := Src;
Pos1 := Pos(Before, Temp);
delete(Temp, 1, Pos1+length(Before));
Pos2 := Pos(After, Temp);
if (Pos1 = 0) or (Pos2 = 0) then
begin
result := '';
Exit;
end;
Pos1 := Pos1 + Length(Before);
result := Copy(Src, Pos1, Pos2);
end;请问这个函数为什么在删除的时候还要加上length(Before). Pos1 不是已经包含了完整的长度了吗? 我用我的代码测试了,是可以,但是这个也可以,我真的看不懂。有本身的长度,还要加上Before 参数的长度。
Pos1 := Pos('www.', 'www.zhuanhou.cn');
结果是1,
delete('www.zhuanhou.cn', 1, Pos1+length(Before));
如果不加length('www.')变为
delete('www.zhuanhou.cn', 1, 1);
你觉得这样对吗
delete(Temp, 1, Pos1+length(Before));如果這句“.“沒空格的話也是錯的,正確的是:
delete(Temp, 1, length(Before));從第一個字符開始刪除length(Before)個字符。
function Fenli(Src: string; Before, After: string): string;//字符串分离函数
var
Pos1, Pos2: Integer;
begin //使用方法:Fenli('www.zhuanhou.cn','www.','.cn') 得到的结果为:zhuanhou
Pos1 := Pos(Before, Src);
Pos2 := Pos(After, Src);
if (Pos1 = 0) or (Pos2 = 0) then
begin
result := '';
Exit;
end;
result := Copy( Src, 1, Pos2-1);
delete(Result, 1, length(Before));
end;
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,UrlMon;type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
Memo1: TMemo;
Edit3: TEdit;
Label1: TLabel;
Edit4: TEdit;
Edit5: TEdit;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}
function DownloadFile(SourceFile, DestFile: string): Boolean; //下载函数
begin //可以下载网页,图片等任何文件,如 上海门禁系统
try
result := UrlDownloadToFile(nil,PChar(SourceFile),PChar(DestFile), 0, nil) = 0;
except
result := False;
end;
end;
function Fenli1(Src: string; Before, After: string): string; //字符串分离函数
var
Pos1, Pos2: Word;
Temp: string;
begin //使用方法:Fenli('www.zhuanhou.cn','www.','.cn') 得到的结果为:zhuanhou
Temp := Src;
Pos1 := Pos(Before, Temp);
delete(Temp, 1, Pos1);
Pos2 := Pos(After, Temp);
if (Pos1 = 0) or (Pos2 = 0) then
begin
result := '';
Exit;
end;
Pos1 := Pos1 + Length(Before);
form1.edit1.Text := Copy(Src, Pos1, Pos2);
end;
function Fenli(Src: string; Before, After: string): string;//字符串分离函数
var
Pos1, Pos2: Word;
Temp: string;
begin //使用方法:Fenli('www.zhuanhou.cn','www.','.cn') 得到的结果为:zhuanhou
Temp := Src;
Pos1 := Pos(Before, Temp);
form1.Edit4.Text:=inttostr(pos1+length(Before));
delete(Temp, 1, Pos1+length(Before));
form1.Edit5.Text:=temp;
Pos2 := Pos(After, Temp);
if (Pos1 = 0) or (Pos2 = 0) then
begin
result := '';
Exit;
end;
Pos1 := Pos1 + Length(Before);
form1.edit1.Text := Copy(Src, Pos1, Pos2);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i:Integer;
LadTxt:TstringList;
begin
//下载文件,我认为这种方法下载的文件其实就相当于webbrowser访问这个地址
//IP.txt 为保存的文件
DownloadFile('http://www.ip138.com/ips.asp?ip='+edit3.text+'&action=2','IP.txt');
LadTxt:=TStringList.Create;
LadTxt.LoadFromFile('IP.txt');
memo1.Lines.LoadFromFile('IP.txt');
//循环读取 IP.txt 文件
for i:=0 to LadTxt.Count -1 do
begin
//如果当前行包含 您的IP地址是:并且包含 ]
if (Pos('本站主数据:',LadTxt[i])>0) and (Pos('参',LadTxt[i])>0) then
begin
edit2.Text:=trim(ladtxt[i]);
edit1.Text:=fenli(trim(LadTxt[i]),':','<'); //赋值 分离得到当前IP地址
Text:='获取外网IP地址成功IP地址为'+edit1.Text;//更改标题
Exit; //退出循环
end;
end;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
Fenli('www.zhuanhou.cn','www.','.cn');
end;end.
Temp := 'BAAC' ;Pos1 := Pos(Before, Temp); //Pos1:= 2 ; 得到的是Before在Temp中出现的第一个位置,即AA中的第一个A出现的位置 2 ,delete(Temp, 1, Pos1+length(Before)); //从Temp中删除掉Before 和Before之前的字符 即 结果为 C如果使用:delete(Temp, 1, Pos1); //得到的结果是AC ;
后面的COPY是 传进去参数 Src的, 你说要不要加? Pos1 := Pos1 + Length(Before);
Copy(Src, Pos1, Pos2);
POS2的变量值在SRC其实是 POS2- POS1 - length(Before)
其实你Copy(Temp, 1, Pos2 - 1); 就是你要的值 zhuanhou
我测试过的。 得到在字符串内的长度,也包涵Before 的。pos1+Before=4.
delete(temp,1,4) 直接就没有了var
pos1:integer;
Before:string;
Temp:string;
begin
Before:= 'AA';
temp:='BAAC' ;
Pos1 := Pos(Before, Temp);
edit1.Text:=inttostr(pos1);
delete(Temp, 1,Pos1+length(Before));
edit2.Text:=temp;end;测试完成。编辑框2获取到的是空的。
pos1+length(Before)加在一起是4
所以delete(temp,1,4);
直接就没有了。来点专业的回答。 POS2的变量值在SRC其实是 POS2- POS1 - length(Before) 当然是他了。pos2是temp-pos1-lengt(Before)得到的长度
得到
其实你Copy(Temp, 1, Pos2 - 1); 这句简直就是胡扯
var
Pos1, Pos2: Word;
Temp: string;
begin //使用方法:Fenli('www.zhuanhou.cn','www.','.cn') 得到的结果为:zhuanhou
Result := '';
Temp := Src;
Pos1 := Pos(Before, Temp);
delete(Temp, 1, length(Before));
Pos2 := Pos(After, Temp);
if Pos2> 0 then
Result := Copy(Temp, 1, Pos2 - 1);
end;你deleite 前面不知道加个pos1干嘛,明显会多删一个。
其实就4行代码的东西被你弄这么复杂。
function GetStr(Str: string; StartTag, EndTag: string): string;
var
StartIndex, EndIndex: Integer;
begin
Result := EmptyStr;
StartIndex := Pos(StartTag, Str);
if StartIndex = 0 then Exit;
Inc(StartIndex, Length(StartTag));
EndIndex := Pos(EndTag, Copy(Str,StartIndex + 1,Length(Str)-StartIndex)) + StartIndex;
Result := Copy(Str, StartIndex, EndIndex - StartIndex);
end;