本文整理自网络,侵删。
function StrStrA(lpFirst, lpSrch: PAnsiChar): PAnsiChar; stdcall; external 'shlwapi.dll' name 'StrStrA';
function StrNCatA(lpFirst, lpSrch: PAnsiChar; cchMax: Integer): PAnsiChar; stdcall; external 'shlwapi.dll' name 'StrNCatA';
function Pos(lpSrch, lpFirst:PAnsiChar):Integer;
begin
Result := DWORD(StrStrA(lpFirst,lpSrch)) - DWORD(lpFirst) + 1;
end;
procedure Delete(lpszStr: PAnsiChar; index, count: Integer);
var
Len :Integer;
begin
Len := lstrlenA(lpszStr);
if len = count then
begin
lstrcpyA(@lpszStr[index - 1], @lpszStr[index + count -1]);
ZeroMemory(@lpszStr[index -1], index + count);
end else begin
lpszStr[index - 1] := #0;
StrNCatA(lpszStr, @lpszStr[index + count - 1], Len);
end;
end;
function Copy(lpszStr:PAnsiChar; index, count:Integer):PAnsiChar;
begin
Result := GetMemory(count - index);
lstrcpynA(Result, @lpszStr[index -1], count);
end;
//----------------------------------例子
procedure TForm1.Button1Click(Sender: TObject);
var
buf, tmp: PAnsiChar;
i:Integer;
begin
buf := GetMemory(10);
ZeroMemory(buf, 10);
StrCopy(buf, 'http://www.lovehuai.cn/');
i := Pos('u', buf);
Delete(buf, i, 2); // 执行后buf = http://www.loveai.cn/
tmp := Copy(buf, 1, 5);
Edit1.Text := buf + ' pos:' + IntToStr(i) + ' tmp:' + tmp;
end;
相关阅读 >>
Delphi strutils.dupestring - 反复字符串
Delphi在设计时设置tstringgrid控件各列的列宽
Delphi程序在win7 win8 win10下自动请求以管理员身份运行
更多相关阅读请进入《Delphi》频道 >>