[delphi技术]常用的几个字符串处理函数

unit StrSub;

interface
uses
  windows, SysUtils, StrUtils;

//获取SubStr在Str中左边的部分字符串,从左起搜索
function GetLeftStr(SubStr, Str: string): string;

//获取SubStr在Str中右边的部分字符串,从左起搜索
function GetRightStr(SubStr, Str: string): string;

//获取SubStr在Str中左边的部分字符串,从右起搜索
function GetLeftEndStr(SubStr, Str: string): string;

//获取SubStr在Str中右边的部分字符串,从右起搜索
function GetRightEndStr(SubStr, Str: string): string;

//取得在LeftStr和RightStr中间的字符串,从左起搜索
function GetStr(LeftStr, RightStr, Str: string): string;

//取得在LeftStr和RightStr中间的字符串,从右起搜索
function GetEndStr(LeftStr, RightStr, Str: string): string;

implementation

function GetLeftStr(SubStr, Str: string): string;
begin
  Result := Copy(Str, 1, Pos(SubStr, Str) - 1);
end;
//-------------------------------------------

function GetRightStr(SubStr, Str: string): string;
var
  i: integer;
begin
  i := pos(SubStr, Str);
  if i > 0 then
    Result := Copy(Str
      , i + Length(SubStr)
      , Length(Str) - i - Length(SubStr) + 1)
  else
    Result := '';
end;
//-------------------------------------------

function GetLeftEndStr(SubStr, Str: string): string;
var
  i: integer;
  S: string;
begin
  S := Str;
  Result := '';
  repeat
    i := Pos(SubStr, S);
    if i > 0 then
    begin
      if Result <> '' then
        Result := Result + SubStr + GetLeftStr(SubStr, S)
      else
        Result := GetLeftStr(SubStr, S);

      S := GetRightStr(SubStr, S);
    end;
  until i <= 0;
end;
//-------------------------------------------

function GetRightEndStr(SubStr, Str: string): string;
var
  i: integer;
begin
  Result := Str;
  repeat
    i := Pos(SubStr, Result);
    if i > 0 then
    begin
      Result := GetRightStr(SubStr, Result);
    end;
  until i <= 0;
end;
//-------------------------------------------
function GetStr(LeftStr, RightStr, Str: string): string;
begin
  Result := GetLeftStr(RightStr, GetRightStr(LeftStr, Str));
end;
//-------------------------------------------
function GetEndStr(LeftStr, RightStr, Str: string): string;
begin
  Result := GetRightEndStr(LeftStr, GetLeftEndStr(RightStr, Str));
end;

end.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值