Delphi 截取字符串函数copy自定义mycopy

作品分类:编程随笔 2018-06-29

public 区域定义:

  function mycopy(const bstr,estr,tstr:string):string;

函数主体:

//bstr:开始字符串
//estr:结束字符串
//tstr:截取来源字符串

function TForm1.mycopy(const bstr, estr, tstr: string): string;
begin
    //截取字符串
  if (bstr<>'')and(estr<>'') then 
  begin
     Result:=Copy(tstr,pos(bstr,tstr)+length(bstr),Pos(estr,tstr)-pos(bstr,tstr)-length(bstr));
  end
  else if (bstr<>'')and(estr='') then
  begin
     Result:=Copy(tstr,pos(bstr,tstr)+length(bstr),Length(tstr)-pos(bstr,tstr)-length(bstr)+1);
  end
  else if (bstr='')and(estr<>'') then
  begin
      Result:=Copy(tstr,0,pos(estr,tstr)-1);
  end
  else if (bstr='')and(estr='') then
  begin
      Result:=tstr;
  end
  else if tstr='' then
  begin
      Result:='';
  end;
end;

使用例子:

var
bstr,estr,tstr:string;
rstr:string;//结果
begin
    tstr:='123456';
    bstr:='1';
    estr:='3';
    rstr:=mycopy(bstr,estr,tstr);
    //返回:'2'(没有单引号)
end;


二维码
意见反馈