有一个字符串是
第一种:345 453; ; ; ; 第二种 453 542;564 654; ; ; 第三种 463 367; ;432 542; ;现在需要对该字符串分割处理,把里面的数字提取出来把它们赋值给数组或自定义变量都可。注意的问题是:每一种有四组数据,每一组数据用分号格开,每一组数据有两项数字。当遇到空值时,也要给数组或变量赋空值。
第一种:345 453; ; ; ; 第二种 453 542;564 654; ; ; 第三种 463 367; ;432 542; ;现在需要对该字符串分割处理,把里面的数字提取出来把它们赋值给数组或自定义变量都可。注意的问题是:每一种有四组数据,每一组数据用分号格开,每一组数据有两项数字。当遇到空值时,也要给数组或变量赋空值。
var
Str : String;
vList : TStringList;
i : Integer;
begin
Str := '345 453; ; ; ;';
vList := TStringList.Create;
try
for i := 0 to 3 do
begin
if Pos(';', Str) then
begin
vList.Add(Copy(Str, 1, Pos(';', Str) - 1));
Str := Copy(Str, Pos(';', Str) + 1, Length(Str));
end;
end;
finally
vList.Free;
end;
end;
function Compose(str: string; Num: Integer; fgf: string = '|'): string;
//例子
//const testStr='001,002,003,004,005';
//result0 := Compose(testStr,',',0); '5';{返回用分隔符分成的断数}
//result1 := Compose(testStr,',',1); '001'
//result2 := Compose(testStr,',',2); '002';
//result3 := Compose(testStr,',',3); '003';
var
TempI: Integer;
ResultNum: Integer;
begin
ResultNum := 0;
if Num = 0 then
begin
while True do
begin
TempI := Pos(fgf, Str);
if TempI <> 0 then
begin
Inc(ResultNum);
Str := Copy(Str, TempI + Length(fgf), Length(Str));
end
else
Break;
end;
Result := IntToStr(ResultNum + 1);
end
else
begin
for TempI := 2 to Num do
begin
ResultNum := Pos(fgf, str {Result});
if ResultNum = 0 then
str := ''
else
str := Copy(str, ResultNum + Length(fgf), Length(str));
end;
Result := Str;
ResultNum := Pos(fgf, str);
if ResultNum <> 0 then
Result := Copy(str, 1, ResultNum - 1);
end;
end;
var
sL: TStringList;
ss: string;
begin
ss:= '463 367; ;432 542; ;';
ss:= replace(ss,';',',',false);
ss:= replace(ss,' ',',',false);
sL := TStringList.Create;
sL.Delimiter := ',';
sL.DelimitedText := ss;
edit1.Text := trim(sL.Strings[0]+' '+sL.Strings[1]);
edit2.Text := trim(sL.Strings[2]+' '+sL.Strings[3]);
edit3.Text := trim(sL.Strings[4]+' '+sL.Strings[5]);
edit4.Text := trim(sL.Strings[6]+' '+sL.Strings[7]);
sl.Free;
end;function Replace(Str,s1,s2:string;CaseSensitive:Boolean):string;
var
i:integer;
s,t:string;
begin
s:='';
t:=str;
repeat
if casesensitive then
i:=pos(s1,t)
else
i:=pos(lowercase(s1),lowercase(t));
if i>0 then
begin
s:=s+Copy(t,1,i-1)+s2;
t:=Copy(t,i+Length(s1),MaxInt);
end
else
s:=s+t;
until i<=0;
result:=s;
end;
第一种分出来的数据是:345 453 '' '' '' '' '' ''
第二种分出来的数据是:453 542、453 542 '' '' '' ''
第三种分出来的数据是:463 367 '' '' 432 542 '' ''如果要把这些数字分别给Edit控件赋值的话, 需要 24个Edit控件.
edit1.Text := trim(sL.Strings[0]);
edit2.Text := trim(sL.Strings[1]);
edit3.Text := trim(sL.Strings[2]);
edit4.Text := trim(sL.Strings[3]);
edit5.Text := trim(sL.Strings[4]);
edit6.Text := trim(sL.Strings[5]);
edit7.Text := trim(sL.Strings[6]);
edit8.Text := trim(sL.Strings[7]);