procedure TForm1.Button1Click(Sender: TObject); function MyStr(ASource:string; Index: Integer): string; var st: TStrings; i: Integer; begin Result := ''; st := TStringList.Create; i :=ExtractStrings([';'], [], PChar(ASource), st); if i > 0 then Result := st.ValueFromIndex[Index]; st.Free; end; begin Caption := MyStr('CD=123;EE=23;KK=266;',0);//0=123,1=23;2=266 end;
{忘记索引可能越界了,修改一下} procedure TForm1.Button1Click(Sender: TObject); function MyStr(ASource: string; Index: Integer): string; var st: TStrings; i: Integer; begin Result := ''; st := TStringList.Create; i := ExtractStrings([';'], [], PChar(ASource), st); if (i > 0) and (Index in [0 .. i - 1]) then Result := st.ValueFromIndex[Index]; st.Free; end;begin Caption := MyStr('CD=123;EE=23;KK=266;', 0) end;
我是楼主,为什么不能返回正确的结果? -----------------要读的文本内容conn.udl如下------------------ [oledb] ; Everything after this line is an OLE DB initstring Provider=SQLOLEDB.1;Password="";Persist Security Info=True;User ID=sa;Initial Catalog=MYDB;Data Source=127.0.0.1 //读取文本文件 function LoadTxtFile(FilePath:String):string; var txtlist:TStringList; i:integer; TxtString:String; begin txtlist:=TStringList.create; txtlist.LoadFromFile(FilePath); for i:=0 to txtlist.count-1 do TxtString:=TxtString+txtlist.strings[i]+#10#13 ; Result:=TxtString; end; //取得开始和结束间的字符 function TxtMid(AllStr:String;StartStr:string;EndStr:string):string; var pos1, pos2: Integer; begin pos1 := pos(StartStr, AllStr); pos2 := pos(EndStr, AllStr); Result:= copy(AllStr, pos1 + 1, pos2 - pos1 - 1); end;//按钮操作 要取得连接文件里的密码 procedure TMainForm.Button2Click(Sender: TObject); begin Showmsg(LoadTxtFile('D:\conn.udl')); ShowMsg(TxtMid(LoadTxtFile('D:\conn.udl'),'Password=',';')); end;
str := 'CD=123;EE=23;KK=266';
var
pos1, pos2: Integer;
s: string;
begin
pos1 := pos('=', str);
pos2 := pos(';', str); s := copy(str, pos1 + 1, pos2 - pos1 - 1);
end;
procedure TForm1.Button1Click(Sender: TObject);
function MyStr(ASource:string; Index: Integer): string;
var
st: TStrings;
i: Integer;
begin
Result := '';
st := TStringList.Create;
i :=ExtractStrings([';'], [], PChar(ASource), st);
if i > 0 then
Result := st.ValueFromIndex[Index];
st.Free;
end;
begin
Caption := MyStr('CD=123;EE=23;KK=266;',0);//0=123,1=23;2=266
end;
{忘记索引可能越界了,修改一下}
procedure TForm1.Button1Click(Sender: TObject);
function MyStr(ASource: string; Index: Integer): string;
var
st: TStrings;
i: Integer;
begin
Result := '';
st := TStringList.Create;
i := ExtractStrings([';'], [], PChar(ASource), st);
if (i > 0) and (Index in [0 .. i - 1]) then
Result := st.ValueFromIndex[Index];
st.Free;
end;begin
Caption := MyStr('CD=123;EE=23;KK=266;', 0)
end;
var
lst: TStringList;
begin
lst := TStringList.Create;
try
lst.LineBreak := ';';
lst.Text := 'CD=123;EE=23;KK=266;';
ShowMessage(lst.Values['CD']);
finally
lst.Free;
end;
end;var
lst: TStringList;
begin
lst := TStringList.Create;
try
lst.Text := StringReplace('CD=123;EE=23;KK=266;',';',#13#10,[rfReplaceAll]);
ShowMessage(lst.Values['CD']);
finally
lst.Free;
end;
end;
-----------------要读的文本内容conn.udl如下------------------
[oledb]
; Everything after this line is an OLE DB initstring
Provider=SQLOLEDB.1;Password="";Persist Security Info=True;User ID=sa;Initial Catalog=MYDB;Data Source=127.0.0.1
//读取文本文件
function LoadTxtFile(FilePath:String):string;
var
txtlist:TStringList;
i:integer;
TxtString:String;
begin
txtlist:=TStringList.create;
txtlist.LoadFromFile(FilePath);
for i:=0 to txtlist.count-1 do
TxtString:=TxtString+txtlist.strings[i]+#10#13 ;
Result:=TxtString;
end;
//取得开始和结束间的字符
function TxtMid(AllStr:String;StartStr:string;EndStr:string):string;
var
pos1, pos2: Integer;
begin
pos1 := pos(StartStr, AllStr);
pos2 := pos(EndStr, AllStr); Result:= copy(AllStr, pos1 + 1, pos2 - pos1 - 1);
end;//按钮操作 要取得连接文件里的密码
procedure TMainForm.Button2Click(Sender: TObject);
begin
Showmsg(LoadTxtFile('D:\conn.udl'));
ShowMsg(TxtMid(LoadTxtFile('D:\conn.udl'),'Password=',';'));
end;
下面的函数改一下function TxtMid(AllStr:String;StartStr:string;EndStr:string):string;
var
pos1, pos2: Integer;
begin
pos1 := pos(StartStr, AllStr);
AllStr:=copy(AllStr,pos1+9,length(AllStr)-(pos1+9)+1);
pos2 := pos(EndStr, AllStr); Result:= copy(AllStr, 1, pos2 - 1);
end;
改了几次都这样,不能showmessage出来吗function LoadTxtFile(FilePath:String):string;