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. | |
[delphi技术]常用的几个字符串处理函数
最新推荐文章于 2021-11-14 14:34:52 发布