I have done this small string class basing myself on QStrings and FastStrings, thus this class is fast and handles several string functions. BTW in order to compile and use this class you will need FastStrings (you can get a copy at
Peter Morris HomePage).
Why Make A String Class? Well Although this Class Lacks Operator Overloading I need it, and weght someone Might Benefit of it.
Update: Added Case Snsitive Startswith and endswith
Fixed: BUGS in Left, Right (Copied from qstrings)
unit IMLCommon; interfaceuses SysUtils, Classes; type TCharSet = Set of Char; TString = class private Buffer: AnsiString; FWordSeparators: TCharset; function GetLength: Integer; function GetRefCount: Integer; function GetCharacter (const Index: Integer): Char; function GetWordSeparators : TCharSet; function GetAsPChar: PChar; function GetAsInteger: Integer; function GetAsWord: Word; function GetWordCount: Integer; procedure SetLength (const Value: Integer); procedure SetCharacter (const Index: Integer; const Value: Char); procedure SetWordSeparators (const Value: TCharSet); procedure SetAsPChar (const Value: PChar); procedure SetAsInteger (const Value: Integer); procedure SetAsWord (const Value: Word); protected function FindEx (const SubString: String; const CaseSensitive: Boolean = True; const StartPos : Integer = 1; Const ForwardSearch: boolean = true): Integer; Function InternalInteger (Var Variable: Integer; Const Highbound, Lowbo und: Integer): Boolean; function InternalWord (Const Index: Integer): String; public function Left (Count: Integer): String; function Rigth (Count: Integer): String; function Mid (const AStart, ACount: Integer): String; function Uppercase: String; function LowerCase: String; function IsEmpty: Boolean; function StartsWith (const AString: String; const CaseSensitive: Boolean = False): Boolean; function EndsWith (const AString: String; const CaseSensitive: Boolean = False) : Boolean; Function Find: String; const startpos: integer = 1; const caasensitive: boolean = true): integer; function findrev (const SUBSTRING: STRING; const startpos: integer =
-1; const CaseSensitive: Boolean = True): Integer; function Contains (const SubString: String; const CaseSensitive: Boolean = True): Boolean; function IsNumber: Boolean; function Append (const Value: String): String; function Prepend ( const Value: String): String; function Remove (const Index, Length: Integer): String; procedure Truncate (const newLength: Integer); procedure Fill (const AChar: Char; newLength: Integer = -1); procedure Insert (const Index: Integer; const AString: String); property Content: String Read Buffer Write Buffer; property RefCount: Integer read GetRefCount; property Characters [const Index: Integer]: Char read GetCharacter write SetCharacter; default; property Words [const Index: Integer ]: String read InternalWord; property WordCount: Integer read GetWordCount; property Length: Integer read GetLength write SetLength; property WordSeparators: TCharSet read GetWordSeparators write SetWordSeparators; property AsPChar : PChar read GetAsPChar write SetAsPChar; property AsInteger: Integer read GetAsInteger write SetAsInteger; property AsWord: Word read GetAsWord write SetAsWord; end; implementationuses {JclStrings,} FastStrings; {TString} function TString.EndsWith (const AString: String; const CaseSensitive: Boolean): Boolean; begin if CaseSensitive then Result: = AnsiSameStr (AString, rigth (System.Length (AString))) else Result: = AnsiSameText (AString, rigth (System.Length (AString))); end; function TString. FindEx (const SubString: String; const CaseSensitive: Boolean; const StartPos: Integer; const ForwardSearch: Boolean): Integer; begin Result: = SmartPos (SubString, Buffer, CaseSensitive, StartPos, ForwardSearch); end;
Procedure Tstring.Fill (Const Achar: Char; Newlength: Integer); Begin IF (Newlength <0) THEN BEGIN if Length> 0 Then Fillchar (Buffer [1], Length, ORD (ACHAR) ELSE Fillchar (Buffer [1] , 1, ORD (ACHAR); Else Begin SetLength; Fillchar (Buffer [1], Length, ORD (ACHAR); End; End; Function Tstring.Find (const Substring: string; const startpos: integer ; const CaseSensitive: Boolean): Integer; begin Result: = FindEx (SubString, CaseSensitive, StartPos); end; function TString.FindRev (const SubString: String; const StartPos: Integer; const CaseSensitive: Boolean): Integer; var RealStartPos: Integer; begin if StartPos <0 then RealStartPos: = Length else RealStartPos: = StartPos; Result: = FindEx (SubString, CaseSensitive, RealStartPos, False); end; function TString.GetAsPChar: PChar; begin Result: = PChar (Buffer); End; function tstring.getCharacter (const index: integer): char; begin if ISempty or (Index> length) Then Result: = # 0 else result: = buffer [index] End; function tstring.getlength: integer; var p: pointer; begin result: = 0; if pointer (buffer) <> nil dam p: = Pointer (Integer (buffer) - 4); result: = Integer (p ^) and (not $ 80000000 shr 1); end; end; function tstring.isempty: boolean; begin result: = (length = 0); end; function tstring.Left (count: integer): string; beginiff Count> Length The Result: = Buffer Else Begin System.SetLength (Result, Count); Move (Buffer [1], Result [1], Count); end; end; function tstring.lowercase: string; begin result: = sysutils .Lowercase (buffer); end; function tstring.mid (const): string; begin result: =
Copy (Buffer, ASTART, ACOUNT); End; Function Tstring.Rigth (Count: Integer): String; Begin If Count> Length Then Res: = 'Else Begin System.SetLength; Move (Buffer [Length - (count-1)], result [1], count; end; end; procedure: pchar); begin buffer: = value; end; procedure tstring.setcharacter (const index: integer; const Value: char); begin if not ipsempty kiln f i
Begin Result: = true; for i: = 1 to Length Do IF Not (GetCharacter (i) in ['0'. '9', ' ', '-', '.']) THEN BEGIN RESULT: = false; Exit; end; end; function TString.InternalInteger (var Variable: Integer; const HighBound, lowBound: Integer): Boolean; var ErrorCode: Integer; begin Result: = false; if IsEmpty or not IsNumber then Exit; Val (Buffer , Variable, ErrorCode); Result: = (Errorcode = 0) and ((Variable> = lowBound) and (Variable <= HighBound)); end; function TString.GetAsInteger: Integer; begin InternalInteger (Result, High (Integer), Low; procedure tstring.setasinteger (const value: integer); Begin Str (Value, Buffer); End; Function Tstring.getasword: Word; Var TMP: Integer; Begin IF INTERINTEGER (TMP, 0, 65535) ) then Result: = Tmp else Result: = 0; end; procedure TString.SetAsWord (const Value: Word); begin Str (Value, Buffer); end; function TString.GetWordSeparators: TCharSet; begin Result: = FWordSeparators; end; Procedure Tstring.SetWordSeparator (Const Value: tcharset; begin if fwordseparators: = value; end; function tstring.internalWord (const index: integer): string; var i, j, n: integer; l: longword; p: pchar; a : Boolean; begin if (index <= 0) THEN EXIT; L: = Length; p: = pointer (buffer); A: = false; n: = 1; for i: = 1 to l do begin if not (p ^ in WordseParators) THEN BEGIN INDEX THEN BEGIN N: = L 1; INC (P); for J: = i