如何让edit1 edit2 edit3显示load的txt文件,格式进来看(100分)

  • 主题发起人 主题发起人 54weige
  • 开始时间 开始时间
5

54weige

Unregistered / Unconfirmed
GUEST, unregistred user!
如何让edit1 edit2 edit3显示load的txt文件,txt格式是xx dd ff每一行不一样,但是都是xx dd ff的 跪谢
 
你是不是想每行显示到edit1,edit2,edit3,<br>var fp:filetext;<br> &nbsp; tstr:string;<br>assignfile(fp,'c:/test.txt');<br>reset(fp);<br>readln(fp,tstr);<br>edit1.text:=copy(tstr,1,2);<br>edit2.text:=copy(tstr,4,2);<br>edit3.text:=copy(tstr,7,2);
 
楼上用的方法也可以~不过单纯的用copy有点不安全~按你说的只能对Asc||编码的好用~如果中文就可能出错~建议还是先检查一下空格的位置以后再copy~
 
试试 ExtractStrings() 吧 ,它可以把 xx dd ff 按空格分割成三行保存到一个StringList中,然后可以<br> &nbsp;edit1.text:= strlist[0];<br> &nbsp;edit2.text:= strlist[1];<br> &nbsp;edit3.text:= strlist[2];
 
procedure TForm1.Button1Click(Sender: TObject);<br>var strs:tstrings;<br> &nbsp; &nbsp;str:string;<br>begin<br> &nbsp;str:='aa bb cc';<br> &nbsp;strs:=tstringlist.Create;<br> &nbsp;strs.Delimiter:=' ';<br> &nbsp;strs.DelimitedText:=str;<br> &nbsp;edit1.Text:=strs[0];<br> &nbsp;edit2.Text:=strs[1];<br> &nbsp;edit3.Text:=strs[2];<br> &nbsp;freeandnil(strs);<br>end;
 
是空格分割吗
 
tyzhang是高手,向高手学习
 
用空格作为分割符号
 
是啊,有些空格多一点,有些少一点而且有中文!
 
来看看啊.
 
方法笨点,但能实现我自己的需求,大家可以优化一下<br>n:=1;<br> &nbsp;i := 0;<br> &nbsp;j := 1;<br> &nbsp;while i&lt;=length(instr) do<br> &nbsp;begin<br> &nbsp; &nbsp;i := i+1;<br> &nbsp; &nbsp;if (instr=' ') then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;strTmp := copy(instr,j,i-j);<br> &nbsp; &nbsp; &nbsp;if (strTmp&lt;&gt;' ') and (strTmp&lt;&gt;' ') then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;if n=1 then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dh:=trim(strTmp)<br> &nbsp; &nbsp; &nbsp; &nbsp;else if n=2 then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;zh:=trim(strTmp)<br> &nbsp; &nbsp; &nbsp; &nbsp;else if n=3 then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pq:=trim(strTmp)<br> &nbsp; &nbsp; &nbsp; &nbsp;else if n=4 then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;bh:=trim(strTmp)<br> &nbsp; &nbsp; &nbsp; &nbsp;else if n=5 then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;je:=StrToFloat(trim(strTmp))<br> &nbsp; &nbsp; &nbsp; &nbsp;else if n=6 then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rq:=trim(strTmp)<br> &nbsp; &nbsp; &nbsp; &nbsp;else if n=7 then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;xy:=trim(strtmp)<br> &nbsp; &nbsp; &nbsp; &nbsp;else if n=8 then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lx:=trim(strtmp);<br> &nbsp; &nbsp; &nbsp; &nbsp;n:=n+1;<br> &nbsp; &nbsp; &nbsp; &nbsp;j:=i;<br> &nbsp; &nbsp; &nbsp; &nbsp;strtmp:=' ';<br> &nbsp; &nbsp; &nbsp;end<br> &nbsp; &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp; j:=i;<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br> &nbsp;strTmp := copy(instr,j,i-j);<br> &nbsp;lx:=trim(strtmp);
 
贴一个我自己的分隔函数(有些函数历史悠久)<br> function str_dispart(str:string;var strS:TstringList;disStr:string):integer;<br> &nbsp; &nbsp; &nbsp;{将str按disStr 分开并放入strS}<br>和相关函数<br> &nbsp; procedure DelRepStr(var str:string;substr:string);<br> &nbsp; {删除str中连续重复为substr字符串,只保留一个}<br> &nbsp; procedure DelFirststr(var str:string;substr:string);<br> &nbsp; {删除str头部所有为substr的字符串}<br>function str_dispart(str:string;var strS:TstringList;disStr:string):integer;<br>var<br> i,j:integer;<br> st,stt:string;<br>begin<br> &nbsp;strS.Clear;<br> &nbsp;stt:=str;<br> &nbsp; &nbsp; stt:=stt+disStr;<br> &nbsp; &nbsp;delRepStr(stt,disStr);//删除连续的分隔,只保留一个。<br> &nbsp; &nbsp;if stt=disStr then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;result:=0;<br> &nbsp; &nbsp; &nbsp;exit;<br> &nbsp; &nbsp; &nbsp; end;<br> &nbsp; &nbsp;delfirstStr(stt,disStr);//删除起始分隔。<br> &nbsp; &nbsp;//delendStr(str,disStr);//删除末尾分割。<br> &nbsp; &nbsp; j:=length(disStr);<br> &nbsp; &nbsp;i:=pos(disStr,stt);<br> &nbsp; while i&lt;&gt;0 do<br> &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; st:=copy(stt,0,i-1);<br> &nbsp; &nbsp; &nbsp; &nbsp;strS.add(st);<br> &nbsp; &nbsp; &nbsp;delete(stt,1,i+j-1);<br> &nbsp; &nbsp; &nbsp;i:=pos(disStr,stt);<br> &nbsp; &nbsp; &nbsp; end;//while i&lt;&gt;0 &nbsp; &nbsp;strings<br> &nbsp; &nbsp; &nbsp; result:=strS.count;<br> &nbsp;end;<br><br> &nbsp; procedure DelRepStr(var str:string;substr:string);<br> &nbsp; var<br> &nbsp; &nbsp;st:string;<br> &nbsp; &nbsp;i,j:integer;<br> &nbsp;begin<br> &nbsp; &nbsp; &nbsp;st:=substr+substr;<br> &nbsp; &nbsp; &nbsp;j:=length(substr);<br> &nbsp; &nbsp; &nbsp;i:=pos(st,str);<br> &nbsp; &nbsp; &nbsp;while i&lt;&gt;0 do<br> &nbsp; &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; &nbsp; delete(str,i,j);<br> &nbsp; &nbsp; &nbsp; &nbsp; i:=pos(st,str);<br> &nbsp; &nbsp; &nbsp; &nbsp; end;<br> &nbsp; &nbsp; end;<br>procedure DelFirststr(var str:string;substr:string);<br>var<br> st:string;<br> i:integer;<br>begin<br> i:=length(substr);<br> &nbsp;st:=copy(str,1,i);<br> &nbsp;while st=substr do<br> &nbsp; &nbsp;begin<br> &nbsp;delete(str,1,i);<br> &nbsp; st:=copy(str,1,i);<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br>再来一个<br> &nbsp; function DelRepStr_Quotness(var s:string;substr:string):integer;<br> &nbsp; {删除str中连续重复为substr字符串,只保留一个引号内的不删,<br> &nbsp; 若引号孤独则返回孤独引号所在位置,否则返回0}<br>function DelRepStr_Quotness(var s:string;substr:string):integer;<br>var<br> &nbsp;st,st_leader,st_tmp:string;<br> &nbsp;pos_duble,pos_single:integer;<br> &nbsp; TorF:boolean;<br> begin<br> result:=0;<br> Torf:=True;<br> st_leader:='';<br> &nbsp;st:=s;<br> &nbsp;while TorF do<br> &nbsp; &nbsp;begin<br> &nbsp; pos_duble:=pos('&quot;',st);<br> &nbsp; &nbsp;pos_single:= pos('''',st);<br> &nbsp;if &nbsp; (pos_duble&gt;0) or (pos_single&gt;0) &nbsp; &nbsp;then &nbsp;//两种引号至少有一个<br> &nbsp; &nbsp;if &nbsp;(pos_duble&gt;0)and<br> &nbsp; &nbsp;((pos_duble&lt; pos_single) or (pos_single=0)) then &nbsp;//双引号在前 或只有双引号<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; st_tmp:=copy(st,1,pos_duble);<br> &nbsp; &nbsp; &nbsp; DelRepstr(st_tmp,substr);<br> &nbsp; &nbsp;st_leader:=st_leader+st_tmp;<br> &nbsp; &nbsp; &nbsp; &nbsp;delete(st,1,pos_duble);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pos_duble:=pos('&quot;',st);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if &nbsp;pos_duble=0 then &nbsp;//双引号不匹配<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s:= st_leader+st;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result:=length(st_leader)-1;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end; //end if &nbsp;pos_duble=0<br> &nbsp; &nbsp; &nbsp;st_tmp:=copy(st,1,pos_duble);<br> &nbsp; &nbsp; st_leader:=st_leader+st_tmp;<br> &nbsp; &nbsp;delete(st,1,pos_duble);<br> &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; //end &nbsp; if pos_duble&lt; pos_single<br> &nbsp; &nbsp; &nbsp;else &nbsp;begin &nbsp;// &nbsp;单引号在前或只有单引号<br> &nbsp; &nbsp; &nbsp;st_tmp:=copy(st,1,pos_single);<br> &nbsp; &nbsp; &nbsp; DelRepstr(st_tmp,substr);<br> &nbsp; &nbsp;st_leader:=st_leader+st_tmp;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; delete(st,1,pos_single);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pos_single:=pos('''',st);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if &nbsp;pos_single=0 then &nbsp;//单引号不匹配<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;s:= st_leader+st;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result:=length(st_leader)-1;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exit;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end; //end if &nbsp;pos_duble=0<br> &nbsp; &nbsp; &nbsp;st_tmp:=copy(st,1,pos_single);<br> &nbsp; &nbsp; st_leader:=st_leader+st_tmp;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; delete(st,1,pos_single);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end &nbsp;//end &nbsp;if pos_duble&gt; pos_single<br> &nbsp; &nbsp; &nbsp; &nbsp;else TorF:=false;<br> &nbsp; end; // end while<br> &nbsp; &nbsp; &nbsp; DelRepstr(st,substr);<br> &nbsp; &nbsp; &nbsp;st_leader:=st_leader+St;<br> &nbsp; &nbsp;s:= st_leader;<br> &nbsp;end;
 
接受答案了.
 
后退
顶部