您的位置:首页 > 编程语言 > Delphi

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

2005-08-23 14:52 961 查看
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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: