unit StringMacro;

interface

uses Windows, Messages, SysUtils, Classes;
function InStr(p: Integer; s, ts: string): LongInt;
function DownInStr(p: Integer; s, ts: string): LongInt;
function GetStrCount(s, ts: string): Integer;
procedure FindToken(str, sep: string; tl: TStringList);
function TextToNumber(s: string): LongInt;

implementation

//Suchen

function InStr(p: Integer; s, ts: string): LongInt;
var ip: Integer;

begin
ip:=Pos(ts, Copy(s, p, Length(s)-p+1)); if ip>0 then result:=p-1+ip else result:=0;
end;

function DownInStr(p: Integer; s, ts: string): LongInt;
var cv: Integer;

begin
cv:=0;

while (InStr(cv+1, s, ts)>0) and (InStr(cv+1, s, ts)<=p) do
   cv:=InStr(cv+1, s, ts);

result:=cv;
end;

function GetStrCount(s, ts: string): Integer;
var p: Integer;

begin
p:=0; result:=0;

while InStr(p+1, s, ts)>0 do
begin
   p:=InStr(p+1, s, ts);
   result:=result+1;
end;

end;

procedure FindToken(str, sep: string; tl: TStringList);
var cv, vv: Integer;

begin

str:=Trim(str);

cv:=0; vv:=0;

tl.Clear;

while InStr(cv+1, str, sep)>0 do
begin
   cv:=InStr(cv+1, str, sep);
   tl.Add(Copy(str, vv+1, (cv-1)-vv));
   vv:=cv;
end;

if cv<Length(str) then tl.Add(Copy(str, vv+1, Length(str)-vv));
end;

function TextToNumber(s: string): LongInt;
var cv: Integer;
      vv: LongInt;

begin
result:=0;

s:=Trim(s); if s='' then Exit;

try

vv:=1;
for cv:=Length(s) downto 1 do
   if (ord(s[cv])>=48) and (ord(s[cv])<=57) then
   begin
      result:=result+(ord(s[cv])-48)*vv;
      vv:=vv*10;
   end;

if s[1]='-' then result:=-1*result;

except
   result:=0;
end;

end;

end.

Zurück zur Hauptseite