下面这个函数,以空格为分界符,将一行的文本写入到一个数组里 再根据列值取出,你可以做适当的数据类型转换,固定的文件格式你可以根据楼上的建议读入到一个TStrlist类型里。就如你的例子而言,将你的文件格式读一个TSTringList变量MyList中之后,2002/03/01 Y B 2002/03/02 X B如果你想将第二行,第二列的X读入到变量mystr中,则可以这样调用函数: Mystr:=SlitStr(Mylist[1],2);注意函数以空格为分界符,你可以根据你的文件格式自已修改。 function splitstr(str:string;column:integer):string; var Nowstr:string; tempstr:string; temparray:array of string; iCount:integer; NowPos:integer; begin iCount:=0; NowStr:=trim(str); NowPos:=Pos(' ',Nowstr); while Nowpos<>0 do begin inc(iCount); setlength(temparray,iCount); tempArray[iCount-1]:=copy(Nowstr,1,NowPos-1); NowStr:=copy(NowStr,nowpos+1,length(Nowstr)-nowpos); NowPos:=pos(' ',Nowstr); end; inc(iCount); setlength(temparray,icount); temparray[Icount-1]:=NowStr; result:=temparray[column-1]; end;
str_1:=copy(tmp_str,1,pos('/',tmp_str);//pos找到第一个'/'的位置的函数
tmp_str:=trim(copy(pos('/',tmp_str)+1,200));
str_2:=copy(tmp_str,1,pos('/',tmp_str);
tmp_str:=trim(copy(pos('/',tmp_str)+1,200));
str_2:=trim(copy(tmp_str,1,pos('/',tmp_str));
下面这个函数,以空格为分界符,将一行的文本写入到一个数组里
再根据列值取出,你可以做适当的数据类型转换,固定的文件格式你可以根据楼上的建议读入到一个TStrlist类型里。就如你的例子而言,将你的文件格式读一个TSTringList变量MyList中之后,2002/03/01 Y B
2002/03/02 X B如果你想将第二行,第二列的X读入到变量mystr中,则可以这样调用函数:
Mystr:=SlitStr(Mylist[1],2);注意函数以空格为分界符,你可以根据你的文件格式自已修改。
function splitstr(str:string;column:integer):string;
var
Nowstr:string;
tempstr:string;
temparray:array of string;
iCount:integer;
NowPos:integer;
begin
iCount:=0;
NowStr:=trim(str);
NowPos:=Pos(' ',Nowstr);
while Nowpos<>0 do
begin
inc(iCount);
setlength(temparray,iCount);
tempArray[iCount-1]:=copy(Nowstr,1,NowPos-1);
NowStr:=copy(NowStr,nowpos+1,length(Nowstr)-nowpos);
NowPos:=pos(' ',Nowstr);
end;
inc(iCount);
setlength(temparray,icount);
temparray[Icount-1]:=NowStr;
result:=temparray[column-1];
end;