Delphi code part 2 - Strings

March 6th, 2007 by William Yang

Here is the part of my old delphi codes, its a unit I use frequently for string manipulation. Some are out of date, newer version of Delphi comes with better codes, same are not as smart as other units written by smart Russians, like faststrings, etc. None the less, they are useful if you like to learn the basics of Delphi or Pascal.

Show Code | Download StrMan.pas

  1.  
  2. { ---------------------------------------------------
  3.     String Manager   Copyright (r) by DreamFactory
  4.     Version : 1.75   Author : William Yang
  5.     Last Update 25 - Aug - 97
  6.   --------------------------------------------------- }
  7.  
  8.  
  9. unit StrMan; {String Manager}
  10.  
  11. interface
  12. {-- Declaretion Part --}
  13.  
  14. uses Windows, SysUtils, Math, Classes, Registry;
  15.  
  16. type
  17.  
  18.   TCharSet = set of Char;
  19. const
  20.  
  21.   Decimals = ['0'..'9'];
  22.   FloatNums = ['0'..'9', '.'];
  23.   Operators = ['+', '-', '*', '/'];
  24.   HexDecimals = ['0'..'9', 'A'..'F'];
  25.   Letters = ['a'..'z', 'A'..'Z'];
  26.   Symbols = ['"', '''', '<', '>', '{', '}', '[', ']', '(', ',', ')'];
  27.   Masks : array[1..3] of Char  = ('*', '?', '#');
  28.  
  29. function ReplaceOne(Src, Ch : String; iStart, iCount : Integer ) : String;
  30. function FillStr(Amount : Byte; C : Char) : String;
  31. function TitleCase(SourceStr : String) : String;
  32. function ReplaceAll(Source , Target, ChangeTo : String) : String ;
  33. function Instr(iStart : Integer; Src, Find: String) : Integer;
  34. function CompareStrAry(Source : String; CmpTo : Array of string) : Integer;
  35. function LowCaseStr(S : String) : String;
  36. function LoCase(C : Char) : Char;
  37. procedure StrSplit(SrcStr : String; BreakDownPos : Integer;
  38.   var S1, S2 : String);
  39. function LeftStr(S : String; ToPos : Integer) : String;
  40. function RightStr(S : String; ToPos : Integer) : String;
  41. function CharCount(S : String; C : Char) : Integer;
  42.  
  43. function RemoveChars(S : String; C : TCharSet) : String;
  44. function StrBrief(AString: String; AMaxChar: Integer): String;
  45. function EStrToInt(S : String) : Integer;
  46. function EStrToFloat(S : String) : Real;
  47.  
  48. function LastDir(Dir: String): String;
  49.  
  50. function RPos(C: String; Src : String) : Integer; overload;
  51. function RPos(C: String; Src : String; nStart: Integer) : Integer; overload;
  52.  
  53. function ReturnLine(SList: TStringList; Find: String) : String;
  54. procedure SplitStrC (S:string; C : char; var head, queue : string);
  55. procedure Split(StringList: TStringList; S : string; C : char);
  56. function AppPath: String;
  57.  
  58. function ReadBetween(Src, Mark1, Mark2: String): String;
  59. procedure RemoveQuate(var Src: String);
  60.  
  61. function strFileLoad(const aFile: String): String;
  62. procedure strFileSave(const aFile,aString: String);
  63. function JoinStrings(AStrings: TStrings): String; overload;
  64. function JoinStrings(strings: TStrings; delimiter: String): String; overload;
  65. function AddSlashes(AString: String): String;
  66. function StripSlashes(AString: String): String;
  67.  
  68.  
  69. implementation
  70.  
  71. function StrBrief(AString: String; AMaxChar: Integer): String;
  72. begin
  73.   if Length(AString)<=AMaxChar then
  74.     Result := AString
  75.   else
  76.   begin
  77.     Result := Copy(AString, 0, AMaxChar-3) + '...';
  78.   end;
  79. end;
  80.  
  81. function AddSlashes(AString: String): String;
  82. begin
  83.   Result := ReplaceAll(AString, '''', '\''');
  84.   Result := ReplaceAll(Result, '"', '\"');
  85. end;
  86.  
  87. function StripSlashes(AString: String): String;
  88. begin
  89.   Result := ReplaceAll(AString, '\''', '''');
  90.   Result := ReplaceAll(Result, '\"', '"');
  91. end;
  92.  
  93. function JoinStrings(AStrings: TStrings): String;
  94. var
  95.   i: Integer;
  96. begin
  97.   result := '';
  98.   for i := 0 to AStrings.Count-2 do
  99.   begin
  100.     result := result + AStrings[i] + '\';
  101.   end;
  102.   result := result + AStrings[AStrings.Count-1];
  103. end;
  104.  
  105. function JoinStrings(strings: TStrings; delimiter: String): String;
  106. var
  107.   i: Integer;
  108. begin
  109.   if strings.Count=0 then Exit;
  110.   Result := strings[0];
  111.   if strings.Count>0 then
  112.     for i := 1 to strings.Count-1 do
  113.     begin
  114.       Result := Result + delimiter + strings[i]
  115.     end;
  116. end;
  117.  
  118.  
  119. function strFileLoad(const aFile: String): String;
  120. var
  121.   aStr : TStrings;
  122. begin
  123.   Result:='';
  124.   aStr:=TStringList.Create;
  125.   try
  126.     aStr.LoadFromFile(aFile);
  127.     Result:=aStr.Text;
  128.   finally
  129.     aStr.Free;
  130.   end;
  131. end;
  132.  
  133. procedure strFileSave(const aFile,aString: String);
  134. var
  135.   Stream: TStream;
  136. begin
  137.   Stream := TFileStream.Create(aFile, fmCreate);
  138.   try
  139.     Stream.WriteBuffer(Pointer(aString)^,Length(aString));
  140.   finally
  141.     Stream.Free;
  142.   end;
  143. end;
  144.  
  145. procedure RemoveQuate(var Src: String);
  146. begin
  147.   if Src='' then Exit;
  148.   if Src[1]='"' then Delete(Src, 1, 1);
  149.   if Src[Length(Src)]='"' then Delete(Src, Length(Src), 1);
  150.   if Src[1]='''' then Delete(Src, 1, 1);
  151.   if Src[Length(Src)]='''' then Delete(Src, Length(Src), 1)
  152. end;
  153.  
  154. function ReadBetween(Src, Mark1, Mark2: String): String;
  155. var
  156.   i,j : Integer;
  157. begin
  158.   if Mark1='' then i := 1 else i := Pos(Mark1, Src);
  159.   if Mark2='' then j := Length(Src) else j := InStr(i + Length(Mark1), Src, Mark2);
  160.   if j<=0 then j := Length(Src)+1;
  161.   if (i >0) and (j > 0) then
  162.     Result := Copy(Src, i + Length(Mark1), j-i-Length(Mark1));
  163. end;
  164.  
  165. function AppPath: String;
  166. begin
  167.   Result := ExtractFilepath(ParamStr(0));
  168. end;
  169.  
  170. function LastDir(Dir: String): String;
  171. begin
  172.   if Dir[Length(Dir)]='\' then
  173.     Delete(Dir, Length(Dir), 1);
  174.   Result := RightStr(Dir, RPos('\', Dir)+1);
  175. end;
  176.  
  177. procedure SplitStrC (S:string; C : char; var head, queue : string);
  178. var
  179.     I : integer;
  180.     Quated: Boolean;
  181. begin
  182.    head:=''; queue:='';
  183.    Quated := False;
  184.    for i := 1 to Length(S) do    // Iterate
  185.    begin
  186.     if S[i]='"' then
  187.     begin
  188.       if Quated then
  189.       begin
  190.         if head<>'' then Break;
  191.         Quated := False;
  192.       end
  193.       else
  194.       begin
  195.         if i=1 then Quated := True
  196.       end;
  197.     end
  198.     else if S[i]=C then
  199.       begin Break end
  200.     else
  201.       Head := Head + S[i];
  202.    end;    // for
  203.    delete(S,1,i);
  204.    queue := S;
  205. end;
  206.  
  207. procedure Split(StringList: TStringList; S : string; C : char);
  208. var
  209.   Line: String;
  210. begin
  211.   while S <> '' do
  212.   begin
  213.    SplitStrC(S, C, Line, S);
  214.    StringList.Add(Trim(Line));
  215.   end;
  216. end;
  217.  
  218. function ReturnLine(SList: TStringList; Find: String) : String;
  219. var
  220.   i :Integer;
  221.   s: String;
  222. begin
  223.   Result := '';
  224.   for i := 0 to SList.Count - 1 do
  225.   begin
  226.     s := SList[i];
  227.     if Pos(Find, s) > 0 then
  228.     begin
  229.       Result := SList[i];
  230.       Exit;
  231.     end;
  232.   end;
  233. end;
  234.  
  235. function EStrToFloat(S : String) : Real;
  236. var
  237.   i : Integer;
  238.   r : String;
  239. begin
  240.   r := '';
  241.   for i := 1 to Length(S) do
  242.     if s[i] in FloatNums then
  243.       r := r + s[i];
  244.   if r = '' then
  245.     Result := 0
  246.   else
  247.     Result := StrToFloat(r);
  248. end;
  249.  
  250. function EStrToInt(S: String) : Integer;
  251. var
  252.   i : Integer;
  253.   r : String;
  254. begin
  255.   r := '';
  256.   for i := 1 to Length(S) do
  257.     if s[i] in Decimals then
  258.       r := r + s[i];
  259.   if r = '' then
  260.     Result := 0
  261.   else
  262.     Result := StrToInt(r);
  263. end;
  264.  
  265. function RemoveChars(S : String; C : TCharSet) : String;
  266. var
  267.   j : Integer;
  268. begin
  269. //  Result := S;
  270.   j := 1;
  271.   Result := '';
  272.   while j <= Length(S) do
  273.   begin
  274.     if not (S[j] in C) then
  275.       Result := Result + S[j];
  276.     Inc(j);
  277.   end;
  278. end;
  279.  
  280. function ReplaceOne(Src, Ch : String; iStart, iCount : Integer) : String;
  281. var mResult : String;
  282. begin
  283.   mResult := Src;
  284.   Delete(mResult, iStart, iCount);
  285.   Insert(Ch, mResult, iStart);
  286.   ReplaceOne := mResult;
  287. end;
  288.  
  289. function Instr(iStart : Integer; Src, Find: String) : Integer;
  290. var
  291.   CS : String;
  292. begin
  293.     CS := Copy(Src, iStart, Length(Src)-iStart+1);
  294.     if Pos(Find, CS) <> 0 then
  295.       Result := Pos(Find, CS) + iStart - 1
  296.     else
  297.       Result := 0;
  298. end;
  299.  
  300. function LeftStr(S : String; ToPos : Integer) : String;
  301. begin
  302.   Result := Copy(S, 1, ToPos);
  303. end;
  304.  
  305. function RightStr(S : String; ToPos : Integer) : String;
  306. begin
  307.   Result := Copy(S, ToPos, Length(S) - ToPos + 1);
  308. end;
  309.  
  310. procedure StrSplit(SrcStr : String; BreakDownPos : Integer;
  311.   var S1, S2 : String);
  312. begin
  313.   S1 := LeftStr(SrcStr, BreakDownPos - 1);
  314.   S2 := RightStr(SrcStr, BreakDownPos - 1);
  315. end;
  316.  
  317. function ReplaceAll(Source , Target, ChangeTo : String) : String ;
  318. var
  319. Index: Integer;
  320. Src, Tgt, Cht : String;
  321. begin
  322.   Src := Source;
  323.   Tgt := Target;
  324.   Cht := ChangeTo;
  325.   Index := Pos(Tgt, Src);
  326.   while Index > 0 do
  327.   begin
  328.     Src := ReplaceOne(Src, Cht, Index, Length(Tgt));
  329.     Index := Index + Length(Cht);
  330.     Index := Instr(Index, Src, Tgt);
  331.   end;
  332.   Result := Src;
  333. end;
  334.  
  335. function LoCase(C : Char) : Char;
  336. begin
  337.   if (Ord(C) >= Ord('A')) and (Ord(C) <= Ord('Z')) then
  338.     Result := Chr(Ord(C) - (Ord('A') - Ord('a')))
  339.   else LoCase := C;
  340. end;
  341.  
  342. function LowCaseStr(S : String) : String;
  343. var i : Integer;
  344. begin
  345.   for i := 1 to Length(S) do
  346.     S[i] := LoCase(S[i]);
  347. end;
  348.  
  349. {Make The First Letter Of Each Word To Upper Case}
  350. function TitleCase(SourceStr : String) : String;
  351.  
  352. var
  353.   I : Integer;
  354.   First : boolean;
  355. begin
  356.   Result := SourceStr;
  357.   First := True;
  358.   for I := 1 to Length(SourceStr) do
  359.   begin
  360.     if First then
  361.       Result[I] := UpCase(Result[I])
  362.     else
  363.       Result[I] := LoCase(Result[I]);
  364.     First := False;
  365.     if Result[I] in [' ', '=', '"', '''', ',', ';', '.'] then First := True;
  366.   end;
  367.   TitleCase := Result;
  368. end;
  369.  
  370. {Fill The String With Parameter 'C'}
  371. function FillStr(Amount : Byte; C : Char) : String;
  372. var R : String;
  373.   i : byte;
  374. begin
  375.   for i :=1 to Amount do
  376.     R := R + C;
  377.   Result := R;
  378.  
  379. end;
  380.  
  381. function CompareStrAry(Source : String; CmpTo : Array of string) : integer;
  382. var i : Integer;
  383. begin
  384.   Result := -1;
  385.   for i := Low(CmpTo) to High(CmpTo) do
  386.   begin
  387.     if LowCaseStr(Source) = LowCaseStr(CmpTo[i]) then
  388.     begin
  389.       Result := i;
  390.       Exit;
  391.     end;
  392.   end;
  393. end;
  394.  
  395. function RPos(C: String; Src : String) : Integer;
  396. begin
  397.   result := RPos(C, Src, 1);
  398. end;
  399.  
  400. function RPos(C: String; Src : String; nStart: Integer) : Integer;
  401. var
  402.   i : integer;
  403. begin
  404.   Result := 0;
  405.   for i := Length(Src) downto nStart do
  406.     if Src[i] = C then
  407.     begin
  408.       Result := i;
  409.       Break;
  410.     end;
  411. end;
  412.  
  413. function CharCount(S : String; C : Char) : Integer;
  414. var i : Integer;
  415. begin
  416.   Result := 0;
  417.   for i := 1 to Length(S) do
  418.     if S[i] = C then Result := Result + 1;
  419. end;
  420.  
  421. end.
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  

Leave a Reply