unit Strobj;

interface
const Maxlen = 80;

type
  Tstringobject = object
  public
    Val : string[Maxlen];
    constructor Create;
    procedure Eq(const Dest : string);
    function Length : Byte;
    function Pos(const Suchmuster : string) : Byte;
    function Instr(const Suchmuster : string; Start : Byte) : Byte;
    function Rightpos(const Suchstr : string) : Integer;
    procedure Add(const Dest : string);
    procedure Delete(const Start,Anzahl : Byte);
    procedure Deleteto(const Start,Ende : Byte);
    function Substr(const Start,Ende : Byte) : string;
    function Copy(const Start,Anzahl : Byte) : string;
    procedure Insert(const Dest: string; Start : Byte);
    procedure Replace(const Ersatz : string; const Position : Integer);
    function Doubleval : Extended;
    function Intval : LongInt;
    function Delspace : string;
    function TrimRight : string;
    function Trim : string;
    function Lowcase : string;
    function UpCase : string;
  end;

implementation

constructor Tstringobject.Create;

begin
  Val:='';
end;


procedure Tstringobject.Eq(const Dest : string);

begin
  if System.Length(Dest)<=Maxlen
  then Val:=Dest;
end;

function Tstringobject.Length : Byte;

begin
  Length:=System.Length(Val);
end;

function Tstringobject.Pos(const Suchmuster : string) : Byte;

begin
  Pos:=System.Pos(Suchmuster,Val);
end;

function Tstringobject.Instr(const Suchmuster : string; Start : Byte) : Byte;

begin
  if Start<Length
  then Instr:=System.Pos(Suchmuster,System.Copy(Val,Start,Maxlen))+Start-1
  else Instr:=0;
end;

procedure Tstringobject.Add(const Dest : string);

begin
  Val:=Val+Dest;
end;

procedure Tstringobject.Delete(const Start,Anzahl : Byte);

begin
  if (Start<=Length) and (Start+Anzahl<=Length)
  then System.Delete(Val,Start,Anzahl);
end;

procedure Tstringobject.Deleteto(const Start,Ende : Byte);

begin
  if (Start<=Length) and (Ende<=Length) and (Ende>Start)
  then System.Delete(Val,Start,Ende-Start);
end;

procedure Tstringobject.Insert(const Dest: string; Start : Byte);

begin
  if (Start<=Length) and (System.Length(Dest)+Start<=Maxlen)
  then System.Insert(Dest,Val,Start);
end;

function Tstringobject.Doubleval : Extended;

var Temp : Extended;
    Code : Integer;

begin
  System.Val(Val,Temp,Code);
  if Code=0 then Doubleval:=Temp else Doubleval:=-1.0;
end;

function Tstringobject.Intval : LongInt;

var Temp : LongInt;
    Code : Integer;

begin
  System.Val(Val,Temp,Code);
  if Code=0 then Intval:=Temp else Intval:=-1;
end;

procedure Tstringobject.Replace(const Ersatz : string; const Position : Integer);

var I : Integer;

begin
  if (Position>0) and (System.Length(Ersatz)+Position<=Length)
  then
  begin
    for I:=1 to System.Length(Ersatz) do
    Val[I+Position-1]:=Ersatz[I];
  end;
end;

function Tstringobject.Delspace : string;

var I: Integer;
    Strng : string[Maxlen];

begin
  Strng:=Val;
  I:=System.Pos(' ',Strng);
  while I>0 do
  begin
    System.Delete(Strng,I,1);
    I:=System.Pos(' ',Strng);
  end;
  Delspace:=Strng;
end;


function Tstringobject.TrimRight : string;

var I : Integer;
    Strng : string[Maxlen];

begin
  Strng:=Val;
  I:=Length;
  while (I>0) and (Strng[I]=' ') do
  begin
    System.Delete(Strng,I,1);
    Dec(I);
  end;
  TrimRight:=Strng;
end;

function Tstringobject.Trim : string;

var I : Integer;
    Strng : string[Maxlen];

begin
  Strng:=Val;
  while (System.Length(Strng)>0) and (Strng[1]=' ')
  do  System.Delete(Strng,I,1);
  Trim:=Strng;
end;

function Tstringobject.Rightpos(const Suchstr : string) : Integer;

var I,Len : Integer;
    Flag : Boolean;

begin
  Len:=System.Length(Suchstr);
  if Length<Len then begin Rightpos:=-1; Exit; end;
  I:=Length-Len;
  repeat
    Flag:=System.Copy(Val,I+1,Len)=Suchstr;
    Dec(I);
  until Flag or (I=0);
  if Flag then Rightpos:=I+1 else Rightpos:=0;
end;

function Tstringobject.Lowcase : string;

var Strng : string[Maxlen];
    I     : Integer;

begin
  Strng:='';
  for I:=1 to Length do
  if Val[I] in ['A'..'Z'] then Strng:=Strng+Chr(Ord(Val[I])+32)
  else Strng:=Strng+Val[I];
  Lowcase:=Strng;
end;

function Tstringobject.UpCase : string;

var Strng : string[Maxlen];
    I     : Integer;

begin
  Strng:='';
  for I:=1 to Length do
  if Val[I] in ['a'..'z'] then Strng:=Strng+Chr(Ord(Val[I])-32)
  else Strng:=Strng+Val[I];
  UpCase:=Strng;
end;

function Tstringobject.Substr(const Start,Ende : Byte) : string;

begin
  Substr:=System.Copy(Val,Start,Ende-Start);
end;

function Tstringobject.Copy(const Start,Anzahl : Byte) : string;

begin
  Copy:=System.Copy(Val,Start,Anzahl);
end;

begin
end.


