文本文件内容如下;
XX-User: [email protected]
XX-MsgType: MAIL_READ_MAIL
XX-ClientMac: E0C0A0070300
XX-ServerMac: 981000000111
XX-ClientPort: 64315
XX-ServerPort: 80
XX-ClientIp: 123.243.151.171
XX-ServerIp: 202.108.5.18
XX-Protocol: HTTPMAIL
我想把XX-ClientIp: 123.243.151.171后面的123.243.151.171
读取到listview中的IP项中,因行不是固定的,用行来指定位置不现实.谢谢大家.
XX-User: [email protected]
XX-MsgType: MAIL_READ_MAIL
XX-ClientMac: E0C0A0070300
XX-ServerMac: 981000000111
XX-ClientPort: 64315
XX-ServerPort: 80
XX-ClientIp: 123.243.151.171
XX-ServerIp: 202.108.5.18
XX-Protocol: HTTPMAIL
我想把XX-ClientIp: 123.243.151.171后面的123.243.151.171
读取到listview中的IP项中,因行不是固定的,用行来指定位置不现实.谢谢大家.
var
str:string;
begin
Memo1.Lines.LoadFromFile('');
str := Memo1.Lines.Strings[0];
ShowMessage(Copy(str,Pos(':',str)+1,Length(str) - Pos(':',str)));//冒号后的内容
end;
//uses StrUtilsvar vList:TStringList;
i:Integer;
S:string;
begin
vList:=TStringList.Create;
try
vList.LoadFromFile('D:\Test.txt');
for i:=0 to vList.Count-1 do
begin
S:=vList.Strings[i];
if AnsiContainsText(S,'XX-ClientIp') then
begin
ShowMessage(Copy(S,Pos(':',S)+1,Length(S)));
Break;
end;
end;
finally
vList.Free;
end;
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls;type
TForm1 = class(TForm)
btn1: TButton;
OpenDialog1: TOpenDialog;
lv1: TListView;
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.btn1Click(Sender: TObject);
const
str='ClientIp';
stra=':';
var
i:Integer;
list :TListItem;
strli:TStringList;
begin
if OpenDialog1.Execute then begin
strli:=TStringList.Create;
try
strli.LoadFromFile(OpenDialog1.FileName);
for I := 0 to strli.Count - 1 do begin
if Pos(str,strli.Strings[i])>0 then begin
list :=lv1.Items.Add;
list.Caption :=Trim(Copy(strli.Strings[i],Pos(stra,strli.Strings[i])+1,maxint));
end;
end;
finally
strli.Free;
end; end;end;end.
var
fl:TextFile;
path,s:string;
str:string;
i,j,m,n:Integer;
b:Boolean;
begin
path:=ExtractFilePath(Application.ExeName);
AssignFile(fl,path+'text.txt');
Reset(fl);
b:=False;
repeat
Readln(fl,str);
i:=Pos('XX-ClientIp:',str);
if i>0 then
begin
s:=Copy(str,i+length('XX-ClientIp:'),Length(str)-i-length('XX-ClientIp:')+1);
Self.ListBox1.Items.Add(s);
b:=True;
end;
until
SeekEof(fl) or b=True;
CloseFile(fl);
end;