一个简单的字符处理问题,请各位帮帮, ( 积分: 100 )

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

liumao

Unregistered / Unconfirmed
GUEST, unregistred user!
有 111,222,333,444 字串<br>要将他们反向的添加进 ListView1 里面去,添加到 Listview1 里面后要显示为<br><br>444<br>333<br>222<br>111<br><br>要怎么做?请各位帮下忙~~
 
有 111,222,333,444 字串<br>要将他们反向的添加进 ListView1 里面去,添加到 Listview1 里面后要显示为<br><br>444<br>333<br>222<br>111<br><br>要怎么做?请各位帮下忙~~
 
很简单啊,用copy和pos涵数就能解决了
 
ListView1.items.insert
 
同意1990的.用POS判断,用COPY把其拆开,放到STRINGLIST中,剩下加入就好办了.
 
能不能给个实列代码?我刚学DELPHI没多久~请各位帮下
 
unit Unit1;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> &nbsp;Dialogs, ComCtrls, StdCtrls;<br><br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;Button1: TButton;<br> &nbsp; &nbsp;lv: TListView;<br> &nbsp; &nbsp;procedure Button1Click(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br><br>var<br> &nbsp;Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>t,s:string;<br>p:integer;<br>begin<br>s:='111,222,333,444';<br>while pos(',',s)&lt;&gt;0 do<br> begin<br> p:=pos(',',s);<br> showmessage(s);<br> t:=copy(s,0,p-1);<br>(lv.Items.Insert(0) as Tlistitem).Caption:=t;<br> delete(s,1,p);<br> end;<br> (lv.Items.Insert(0) as Tlistitem).Caption:=s;<br>end;<br><br>end.
 
procedure TForm1.Button1Click(Sender: TObject);<br>var<br>t,s:string;<br>i:integer;<br>begin<br> &nbsp; &nbsp;s:='111,222,333,444';<br> &nbsp; &nbsp;for i:=0 to Length(s) do<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; if s[Length(s)-i] in ['0'..'9'] then<br> &nbsp; &nbsp; &nbsp; t:=t+s[Length(s)-i]<br> &nbsp; &nbsp;else<br> &nbsp; &nbsp;t:=t+#13#10; //在这把t加到你要加的内容中<br>end;<br> &nbsp;showmessage(t);<br>end;<br>end.
 
最快的方法:<br>procedure StrToList(const S: String; vStr: TStrings);<br>type<br> &nbsp;TCharAry = array [1..1] of Char;<br>var<br> &nbsp;N: Integer;<br> &nbsp;P: PChar;<br> &nbsp;Ps: ^TCharAry;<br>begin<br> &nbsp;Ps:= @S[1];<br> &nbsp;vStr.BeginUpdate;<br> &nbsp;try<br> &nbsp; &nbsp;vStr.Clear;<br> &nbsp; &nbsp;for N:= Length(S) downto 1 do if S[N] = ',' then begin<br> &nbsp; &nbsp; &nbsp;P:= @S[N+1];<br> &nbsp; &nbsp; &nbsp;vStr.Add(P);<br> &nbsp; &nbsp; &nbsp;Ps^[N]:= #0; // 提供下一个 0 结尾符<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;if N1 &lt;&gt; 0 then begin<br> &nbsp; &nbsp; &nbsp;P:= @S[1];<br> &nbsp; &nbsp; &nbsp;vStr.Add(P);<br> &nbsp; &nbsp;end;<br> &nbsp;finally<br> &nbsp; &nbsp;vStr.EndUpdate;<br> &nbsp;end;<br>end;
 
function StrToStringList(S, separator: string): TStringList;<br>var<br> &nbsp;tmpList: TStringList;<br> &nbsp;t1, t2: string;<br> &nbsp;M1, M2, M3: string;<br> &nbsp;I, J: integer;<br>begin<br> &nbsp;tmpList := TStringList.Create;<br> &nbsp;t1 := UpperCase(S);<br> &nbsp;t2 := UpperCase(separator);<br> &nbsp;M2 := S;<br> &nbsp;I := AnsiPos(t2, t1);<br> &nbsp;if I = 0 then<br> &nbsp; &nbsp;tmpList.Add(S)<br> &nbsp;else<br> &nbsp;begin<br> &nbsp; &nbsp;J := I;<br> &nbsp; &nbsp;while J &gt; 0 do<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;M3 := copy(M2, 1, J-1);<br> &nbsp; &nbsp; &nbsp;M2 := copy(M2, J+Length(separator), Length(M2)-(J+Length(Separator))+1);<br> &nbsp; &nbsp; &nbsp;tmpList.Add(M3);<br> &nbsp; &nbsp; &nbsp;t1 := UpperCase(M2);<br> &nbsp; &nbsp; &nbsp;J := AnsiPos(t2, t1);<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;if Length(M2) &gt; 0 then<br> &nbsp; &nbsp; &nbsp;tmpList.Add(M2);<br> &nbsp;end;<br> &nbsp;Result := tmpList;<br>end;<br><br>procedure AddToListView;<br>var<br> &nbsp;S: string;<br> &nbsp;AList: TStringList;<br> &nbsp;AnItem: TListItem;<br>begin<br> &nbsp;S := '111,222,333,444';<br> &nbsp;AList := TStringList.Create;<br> &nbsp;try<br> &nbsp; &nbsp;AList := StrToStringList(S, ',');<br> &nbsp; &nbsp;for I := 0 to AList.Count - 1 do<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;AnItem := ListView1.Items.Add;<br> &nbsp; &nbsp; &nbsp;AnItem.Caption := AList; <br> &nbsp; &nbsp;end;<br> &nbsp;finally<br> &nbsp; &nbsp;AList.Free;<br> &nbsp;end;<br>end;
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
489
import
I
后退
顶部