type
TDelFlags = set of (dfDelBefore, dfDelAfter);
//删除ms字符串中endstr子字符串前面或后面的部分字符串
procedure Delstr(var ms: String; endstr: String; Flags: TDelFlags;
bself: Boolean = True);
var
l: Integer;
begin
l := length(endstr);
if pos(AnsiLowercase(endstr), AnsiLowercase(ms))=0 then
begin
exit;
end;
if dfDelBefore in Flags then
begin
if bself then
Delete(ms, 1, pos(AnsiLowercase(endstr), AnsiLowercase(ms)) + l - 1)
else
Delete(ms, 1, pos(AnsiLowercase(endstr), AnsiLowercase(ms)) - 1)
end
else
begin
if bself then
Delete(ms, pos(AnsiLowercase(endstr), AnsiLowercase(ms)), length(ms))
else
Delete(ms, pos(AnsiLowercase(endstr), AnsiLowercase(ms)) + l, length(ms))
end;
end;
//从src主字符串中截取两个字符串(str1和str2)之间的部分字符串
function GetCenterStr(src, str1, str2: string): string;
var
i, i2, i3: integer;
begin
i := 0;
i2 := 0;
i3 := 0;
Delstr(src, str1, [dfDelBefore]);
i := pos(AnsiLowercase(str1), AnsiLowercase(src));
i3 := pos(AnsiLowercase(str2), AnsiLowercase(src));
result := copy(src, i2 + 1, i3 - i2 - 1);
end;
/////////////////////// Delstr函数的升级版如下 ////////////////////////////
function fastStrCopy(S:string;Index:integer;count:integer):string;
var
sLen: Integer;
begin
//此函数类似copy函数,但在拷贝大量字符串时比较copy函数的效率高很多。
Result:=S;
{
拷贝全部字符串
SetLength(Result,length(S));
MoveMemory(@Result[1],@S[1],bytelength(S));
}
//拷贝部分字符串
sLen:=0;
sLen:=length(S);
if(sLen<Index+count) then count:=sLen-Index+1;
SetLength(Result,count);
MoveMemory(@Result[1],@S[Index],count*sizeof(widechar));
end;
function FastStrCopy2(S: String; Index: Integer; Count: Integer): String;
var
p: PChar;
buf: LPCWSTR;
begin
p := PChar(S);
getMem(buf, bytelength(s));
try
inc(p, Index - 1);
//lstrcpyn 复制字符串, 同时指定要复制的长度
lstrcpyn(buf, p, Count + 1);
Result := String(buf);
finally
freemem(buf);
end;
end;
type
TDelFlags = set of (dfDelBefore, dfDelAfter);
function Delstr(var ms: String; endstr: String; Flags: TDelFlags;
bself: Boolean = True): String;
var
l: Integer;
begin
l := length(endstr);
if dfDelBefore in Flags then
begin
if bself then
begin
Result := fastStrCopy(ms, 1, pos(endstr, ms) + l - 1);
Delete(ms, 1, pos(endstr, ms) + l - 1);
end
else
begin
Result := fastStrCopy(ms, 1, pos(endstr, ms) - 1);
Delete(ms, 1, pos(endstr, ms) - 1);
end;
end
else
begin
if bself then
begin
Result := fastStrCopy(ms, pos(endstr, ms), length(ms));
Delete(ms, pos(endstr, ms), length(ms));
end
else
begin
Result := fastStrCopy(ms, pos(endstr, ms) + l, length(ms));
Delete(ms, pos(endstr, ms) + l, length(ms));
end;
end;
end;