var I: integer; s: string; begin s := 'asdgf'; i := pos('d', s); if i> 0 then begin showmessage(Copy(s, 1, i-1)); showMessage(copy(s, i+1, 100)); end;
procedure TForm1.Button1Click(Sender: TObject); Const Splitor='.';//定义分隔符为 . Var aStr,s1,s2:String; atpos,SplitorLen:Integer; begin SplitorLen:=Length(Splitor); aStr:='中国.北京'; atPos:=Pos(Splitor,aStr)-1; S1:=Copy(aStr,1,atPos); atPos:=Pos(Splitor,aStr)+SplitorLen; s2:=Copy(aStr,atPos,Length(aStr)); ShowMessage('分隔为:'+#13+#13+s1+#13+#13+s2); end;
copy(a:string;i,j:integer)函数就可以的
可以用Copy函数来截取字符~~
你可以调用我写的过程: String_Divide_TwoPart(你要分解的串,,子串,前半串,后半串) procedure String_Divide_TwoPart(str:string;str1:string;var firstPart:string;var SecondPart); var pos1:integer; begin pos1:=pos(str,str1);//子串d在总串的位置 firstPart:=copy(str,1,pos1-1); SecondPart:=copy(str,pos1+length(str1)); end;
主要是利用POS取得子串的位置,使用COPY来提取字符串
function SplitStrByFlag(s:string;Flag:string;Head:Bool):string; var iPos:integer; begin result:=''; ipos:=Pos(flag,s); if iPos =0 then exit; if Head then result:=Leftstr(s,iPos-1) else result:=midstr(s,iPos+1,1024);end;
procedure TForm1.Button1Click(Sender: TObject); var i:integer; k:string; begin for i:=1 to length(edit1.text) do begin if copy(edit1.text,i,1)<>'/' then k:=k+copy(edit1.text,i,1) else begin memo1.Lines.Add(k); k:=''; end; end; end;
s: string;
begin
s := 'asdgf';
i := pos('d', s);
if i> 0 then
begin
showmessage(Copy(s, 1, i-1));
showMessage(copy(s, i+1, 100)); end;
Const
Splitor='.';//定义分隔符为 .
Var
aStr,s1,s2:String;
atpos,SplitorLen:Integer;
begin
SplitorLen:=Length(Splitor);
aStr:='中国.北京';
atPos:=Pos(Splitor,aStr)-1;
S1:=Copy(aStr,1,atPos);
atPos:=Pos(Splitor,aStr)+SplitorLen;
s2:=Copy(aStr,atPos,Length(aStr));
ShowMessage('分隔为:'+#13+#13+s1+#13+#13+s2);
end;
procedure String_Divide_TwoPart(str:string;str1:string;var firstPart:string;var SecondPart);
var
pos1:integer;
begin
pos1:=pos(str,str1);//子串d在总串的位置
firstPart:=copy(str,1,pos1-1);
SecondPart:=copy(str,pos1+length(str1));
end;
var
iPos:integer;
begin
result:='';
ipos:=Pos(flag,s);
if iPos =0 then exit; if Head then
result:=Leftstr(s,iPos-1)
else
result:=midstr(s,iPos+1,1024);end;
copy('asdgf',pos('d','asdgf')+1,length('asdgf'))哎,估计是拿不到分了
用copy,left,right都可以.
left('asdgf',1,pos('d','asdgf')-1)
left('asdgf',pos('d','asdgf')+1,length('asdgf'))
right是一樣的
var i:integer;
k:string;
begin
for i:=1 to length(edit1.text) do
begin
if copy(edit1.text,i,1)<>'/' then
k:=k+copy(edit1.text,i,1)
else
begin
memo1.Lines.Add(k);
k:='';
end;
end;
end;