TstringList.commatext:=yourstring; then tstringlist[0]...
如果有逗号,可以用pos的,看下帮助就知道了,很好用的.
//------------------------------------------------------------------------------ // // 分解字符串 // 参数: // SaveSt : 用于保存分解后的字符串 // DetachSt : 要分解的源字符串 // DetachCh : 分界符 // //------------------------------------------------------------------------------ procedure DetachString(var SaveSt: TStringList; const DetachSt, DetachCh : String); var DetachLen: Integer; St, S, Str: String; i: Integer; begin St := DetachSt; S := DetachCh; DetachLen := Length(DetachCh); if SaveSt = NIL then Exit; While True do begin i := Pos(S, St); if i <= 0 then begin if St <> '' then SaveSt.Add(St); break; end; Str := Copy(St, 0, i - 1); St := Copy(St, i + DetachLen, Length(DetachSt) - i); SaveSt.Add(Str); end; end; 用法: var Sts: TStrings; i: Integer; begin Sts := TStringList.Create; Sts.Clear; DetachString(Sts, '05318,2003/09/22,13:24:16', ','); for i := 0 to Sts.Count - 1 do ShowMessage(Sts[i]); Sts.Clear; Sts.Free; end;
可以按下的方法取: 1、先取逗号的位置; 2、从逗号开始截取;如下所示; unit Unit1;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } Function GetString(strSource: String; var str: array of String): Boolean; end;var Form1: TForm1;implementation{$R *.DFM}Function TForm1.GetString(strSource: String; var str: array of String): Boolean; var i: Integer; DHCount: Integer; //共有几个逗号 iPos: Integer; begin DHCount := 0; for i := 0 to length(strSource) do if strSource[i] = ',' then DHCount := DHCount + 1; for i := 0 to DHCount - 1 do begin iPos := Pos(',', strSource); str[i] := Copy(strSource, 1, iPos-1); end; Result := True; end;procedure TForm1.Button1Click(Sender: TObject); var strSource: String; str: array of string; begin strSource := 'qww,www,wwww,ww'; setLength(str, 7); GetString(strSource, str); ShowMessage(str[1]); end;end.
有‘,’做分割符就好办了,用while pos(',',youtext)>0 do temptext:=copy(yourtext,1,pos(',',yourtext)-1)
可以先读到(memo)里(当然你可以生成一个stringlist读取)
memo1.loadfromfile('...');
then
tstringlist[0]...
//
// 分解字符串
// 参数:
// SaveSt : 用于保存分解后的字符串
// DetachSt : 要分解的源字符串
// DetachCh : 分界符
//
//------------------------------------------------------------------------------
procedure DetachString(var SaveSt: TStringList; const DetachSt, DetachCh : String);
var
DetachLen: Integer;
St, S, Str: String;
i: Integer;
begin
St := DetachSt;
S := DetachCh;
DetachLen := Length(DetachCh);
if SaveSt = NIL then
Exit; While True do
begin
i := Pos(S, St);
if i <= 0 then
begin
if St <> '' then
SaveSt.Add(St);
break;
end;
Str := Copy(St, 0, i - 1);
St := Copy(St, i + DetachLen, Length(DetachSt) - i);
SaveSt.Add(Str);
end;
end;
用法:
var
Sts: TStrings;
i: Integer;
begin
Sts := TStringList.Create;
Sts.Clear;
DetachString(Sts, '05318,2003/09/22,13:24:16', ',');
for i := 0 to Sts.Count - 1 do
ShowMessage(Sts[i]);
Sts.Clear;
Sts.Free;
end;
1、先取逗号的位置;
2、从逗号开始截取;如下所示;
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
Function GetString(strSource: String; var str: array of String): Boolean;
end;var
Form1: TForm1;implementation{$R *.DFM}Function TForm1.GetString(strSource: String; var str: array of String): Boolean;
var
i: Integer;
DHCount: Integer; //共有几个逗号
iPos: Integer;
begin
DHCount := 0; for i := 0 to length(strSource) do
if strSource[i] = ',' then
DHCount := DHCount + 1; for i := 0 to DHCount - 1 do
begin
iPos := Pos(',', strSource);
str[i] := Copy(strSource, 1, iPos-1);
end;
Result := True;
end;procedure TForm1.Button1Click(Sender: TObject);
var
strSource: String;
str: array of string;
begin
strSource := 'qww,www,wwww,ww';
setLength(str, 7);
GetString(strSource, str); ShowMessage(str[1]);
end;end.