怎么把这个函数写周全?(80分)

  • 主题发起人 主题发起人 liuyang
  • 开始时间 开始时间
L

liuyang

Unregistered / Unconfirmed
GUEST, unregistred user!
这个函数的作用是把制定的分割符号的东西分割出来<br>比如导入一个文本文件test.txt 里面的内容是0001~11111111111111111111~206~412~50~WH7~7<br><br><br>function TForm1.qsdt(dtfh:Char ;str:String; var Strs:TStrings; Allow:boolean):boolean;<br>var<br>&nbsp; i:Integer;<br>&nbsp; Temp:String;<br>begin<br>&nbsp; Result := false;<br>&nbsp; Temp := '';<br>&nbsp; if ((dtfh = '') or (str='')) then exit;<br>&nbsp; Strs.Clear;<br>&nbsp; for i:=1 to Length(str) do<br>&nbsp; &nbsp; if Str&lt;&gt;dtfh then Temp := Temp+Str<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; if I&lt;&gt;1 then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; if Temp&lt;&gt;'' then Strs.Add(Temp)<br>&nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if Allow then Strs.Add( dtfh );<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Temp := '';<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<br>&nbsp; if Temp&lt;&gt;'' then Strs.Add(Temp);<br>&nbsp; Result:= True;<br>end;<br><br>拿这个测试<br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; jilu:TStringList;<br>&nbsp; str:TStrings;<br>&nbsp; i:integer;<br>begin<br>&nbsp; jilu := TStringList.Create;<br>&nbsp; str := TStringList.Create;<br>&nbsp; jilu.LoadFromFile('i:/test.txt');<br>&nbsp; showmessage(inttostr(jilu.Count));<br><br>&nbsp; for i:=0 to jilu.Count-1 do<br>&nbsp; begin<br>&nbsp; &nbsp; &nbsp; qsdt('~',jilu,str,true);<br>&nbsp;<br>&nbsp;showmessage(str[3]);<br>&nbsp; end;<br>&nbsp;end;<br><br>这个时候显示的应该是412<br><br>但要是分割符号为两个的时候就不好处理了。比如符号换成$$<br>(字段有空的情况则$$$$),<br>请问怎么改上面的函数?
 
sl.Text:=stringReplace('0001~11111111111111111111~206412~50~WH7~7','~',#13,[rfReplaceAll, rfIgnoreCase])<br>
 
var<br>&nbsp;sl:TstringList;<br>begin<br>&nbsp;sl:=TstringList.Create;
 
for i:=0 to length(string) do<br>&nbsp; &nbsp;if string&lt;&gt;'~' then <br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; tmp:= tmp + string;<br>&nbsp; &nbsp;end;<br><br>这样好像要简单点吧???
 
ok -----<br>stringreplace一个函数搞定——何必这么复杂呢?<br><br>procedure TForm1.BitBtn1Click(Sender: TObject);<br>var<br>&nbsp;jilu:TStringList;<br>&nbsp;str:TStrings;<br>&nbsp;i:integer;<br>begin<br>&nbsp;jilu := TStringList.Create;<br>&nbsp;str := TStringList.Create;<br>&nbsp;jilu.LoadFromFile('i:/test.txt');<br>&nbsp;showmessage(inttostr(jilu.Count));<br><br>&nbsp;for i:=0 to jilu.Count-1 do<br>&nbsp; &nbsp;str.text:=stringreplace(jilu,'~',#13,[rfReplaceAll]);//<br>&nbsp; &nbsp;//如果是'$$',则str.text:=stringreplace(jilu,'$$',#13,[rfReplaceAll]);//<br><br>&nbsp;showmessage(str.text);//看看结果就知道了<br>&nbsp;str.free;<br>&nbsp;jilu.free;<br>end;<br><br>
 
//我自已写的一个函数,一直都在用!<br>//字符串Str以Ch分隔成几段小字符串,该函数是将这些小字符串提取出来并保存在MyString中<br>function GetStrs(const Str, Ch: string; var Mystring: TStrings) : boolean;<br>var<br>&nbsp; sit, n &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: integer;<br>&nbsp; S &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : string;<br>begin<br>&nbsp; Result := True;<br>&nbsp; MyString.Clear;<br>&nbsp; S := Str;<br>&nbsp; n := Length(Ch);<br>&nbsp; while True do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; if Pos(Ch, S) = 0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MyString.Add(S);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Break;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; sit := Pos(Ch, S);<br>&nbsp; &nbsp; &nbsp; MyString.Add(Copy(S, 1, sit - 1));<br>&nbsp; &nbsp; &nbsp; S := Copy(S, sit + n, Length(S));<br>&nbsp; &nbsp; end;<br>&nbsp; if MyString.Count &lt; 2 then Result := False;<br>end;<br>
 
多人接受答案了。
 
后退
顶部