function SubStrs(const S1, S2 : string) : string; var I : integer; begin I := Pos(S2, S1); Result := S1; if I <> 0 then begin Result := Copy(S1, 1, I-1); Result := Result + Copy(S1, I+Length(S2), MaxInt);//MaxInt可以用的,没问题 end; end;procedure TForm1.Button1Click(Sender: TObject);//以下是测试 var S1, S2 : string; begin S1 := '0123456789'; S2 := '0123'; ShowMessage(SubStrs(S1,S2));// '456789' S1 := '0123456789'; S2 := '456'; ShowMessage(SubStrs(S1,S2));// '0123789' S1 := '0123456789'; S2 := '789'; ShowMessage(SubStrs(S1,S2));// '0123456' S1 := '0123456789'; S2 := 'abcde'; ShowMessage(SubStrs(S1,S2));// '0123456789' S1 := '0123456789'; S2 := '0123456789'; ShowMessage(SubStrs(S1,S2));// '' end;
给你写了另外一种形式的函数体,具体哪个好用,你自己选择吧。测试代码同上。function SubStrs(const S1, S2 : string) : string; var I : integer; S : string; begin I := Pos(S2, S1); S := S1; if I <> 0 then begin Move(S[I+Length(S2)], S[I], Length(S1)-I-Length(S2)+1); SetLength(S, Length(S1)-Length(S2)); end; Result := S; end;
var
I : integer;
begin
I := Pos(S2, S1);
Result := S1;
if I <> 0 then
begin
Result := Copy(S1, 1, I-1);
Result := Result + Copy(S1, I+Length(S2), MaxInt);//MaxInt可以用的,没问题
end;
end;procedure TForm1.Button1Click(Sender: TObject);//以下是测试
var
S1, S2 : string;
begin
S1 := '0123456789';
S2 := '0123';
ShowMessage(SubStrs(S1,S2));// '456789' S1 := '0123456789';
S2 := '456';
ShowMessage(SubStrs(S1,S2));// '0123789' S1 := '0123456789';
S2 := '789';
ShowMessage(SubStrs(S1,S2));// '0123456' S1 := '0123456789';
S2 := 'abcde';
ShowMessage(SubStrs(S1,S2));// '0123456789' S1 := '0123456789';
S2 := '0123456789';
ShowMessage(SubStrs(S1,S2));// ''
end;
var
I : integer;
S : string;
begin
I := Pos(S2, S1);
S := S1;
if I <> 0 then
begin
Move(S[I+Length(S2)], S[I], Length(S1)-I-Length(S2)+1);
SetLength(S, Length(S1)-Length(S2));
end;
Result := S;
end;