11{
2- * This file was generated from the DelphiDabbler Code Snippets collection.
3- *
4- * See https://github.com/delphidabsbler/code-snippets/tree/master/LICENSE.md for
5- * full license & copyright information.
2+ * This unit was generated automatically. It incorporates a selection of source
3+ * code taken from the Code Snippets Database at
4+ * https://github.com/delphidabbler/code-snippets.
5+ *
6+ * The unit is copyright © 2005-2025 by Peter Johnson & Contributors and is
7+ * licensed under the MIT License (https://opensource.org/licenses/MIT).
8+ *
9+ * Generated on : Sat, 05 Apr 2025 12:14:56 GMT.
10+ * Generated by : DelphiDabbler CodeSnip Release 4.24.0.
11+ *
12+ * The latest version of CodeSnip is available from the CodeSnip GitHub project
13+ * at https://github.com/delphidabbler/codesnip.
614}
715
816unit UStringCatSnippets;
917
1018{ $IFNDEF FPC}
1119 { $IFDEF CONDITIONALEXPRESSIONS}
20+ { $IF CompilerVersion >= 24.00}
21+ { $LEGACYIFEND ON}
22+ { $IFEND}
1223 { $IF CompilerVersion >= 14.00}
1324 { $WARN SYMBOL_PLATFORM OFF}
1425 { $WARN SYMBOL_DEPRECATED OFF}
2940interface
3041
3142uses
32- SysUtils, StrUtils, Classes, Windows ;
43+ SysUtils, Classes, StrUtils ;
3344
34- function StripAccelChars (const S: string): string;
45+ {
46+ Checks if the string Value contains a valid numeric value and returns True if
47+ so or False if not.
48+ If AllowFloat is true then Value may contain a floating point number,
49+ otherwise it must be an integer. If TrimWhiteSpace is True any white space
50+ surrounding Value is trimmed before testing.
51+ }
52+ function IsNumeric (Value : string; const AllowFloat: Boolean;
53+ const TrimWhiteSpace: Boolean = True): Boolean;
54+
55+ {
56+ Splits the string StrToParse into segments separated by Delimiter and stores
57+ each segment in turn in string list Words, replacing any existing content.
58+ }
59+ procedure ParseStr (const StrToParse: string; const Delimiter: Char;
60+ const Words: Classes.TStringList);
3561
62+ {
63+ Returns a random string from the given non-empty string list.
64+ An EArgumentException exception is raised if the string list is empty.
65+ }
66+ function RandomString (const SL: Classes.TStrings): string; overload;
67+
68+ {
69+ Returns the reverse of the given string.
70+ }
3671function ReverseStr (S: string): string;
3772
73+ {
74+ Returns the reverse of the given string.
75+ }
3876function ReverseStrR (const S: string): string;
3977
40- function IsNumeric (Value : string; const AllowFloat: Boolean;
41- const TrimWhiteSpace: Boolean = True): Boolean;
42-
78+ {
79+ Splits the string AText into segments separated by Delimiter and creates and
80+ returns a string list containing the segments.
81+ The caller is responsible for freeing the returnd string list object.
82+ }
4383function SplitString (const AText, ADelimiter: string): Classes.TStringList;
4484
45- procedure ParseStr (const StrToParse: string; const Delimiter: Char;
46- const Words: Classes.TStringList);
85+ {
86+ Strips all accelerator ('&') characters from the given string and returns the
87+ resulting string.
88+ }
89+ function StripAccelChars (const S: string): string;
4790
4891implementation
4992
50- function StripAccelChars (const S: string): string;
93+ {
94+ Checks if the string Value contains a valid numeric value and returns True if
95+ so or False if not.
96+ If AllowFloat is true then Value may contain a floating point number,
97+ otherwise it must be an integer. If TrimWhiteSpace is True any white space
98+ surrounding Value is trimmed before testing.
99+ }
100+ function IsNumeric (Value : string; const AllowFloat: Boolean;
101+ const TrimWhiteSpace: Boolean = True): Boolean;
102+ var
103+ ValueInt: Int64; // dummy integer value
104+ ValueFloat: Extended; // dummy float value
51105begin
52- Result := SysUtils.StringReplace(
53- S, ' &' , SysUtils.EmptyStr, [SysUtils.rfReplaceAll]
54- );
106+ if TrimWhiteSpace then
107+ Value := SysUtils.Trim(Value );
108+ // Check for valid integer
109+ Result := SysUtils.TryStrToInt64(Value , ValueInt);
110+ if not Result and AllowFloat then
111+ // Wasn't valid as integer, try float
112+ Result := SysUtils.TryStrToFloat(Value , ValueFloat);
113+ end ;
114+
115+ {
116+ Splits the string StrToParse into segments separated by Delimiter and stores
117+ each segment in turn in string list Words, replacing any existing content.
118+ }
119+ procedure ParseStr (const StrToParse: string; const Delimiter: Char;
120+ const Words: Classes.TStringList);
121+ var
122+ TmpInStr: string;
123+ begin
124+ TmpInStr := StrToParse;
125+ Words.Clear;
126+ if Length(TmpInStr) > 0 then
127+ begin
128+ while Pos(Delimiter, TmpInStr) > 0 do
129+ begin
130+ Words.Append(Copy(TmpInStr, 1 , Pos(Delimiter, TmpInStr) - 1 ));
131+ Delete(TmpInStr, 1 , Pos(Delimiter, TmpInStr));
132+ end ;
133+ Words.Append(TmpInStr);
134+ end ;
55135end ;
56136
137+ {
138+ Returns a random string from the given non-empty string list.
139+ An EArgumentException exception is raised if the string list is empty.
140+ }
141+ function RandomString (const SL: Classes.TStrings): string; overload;
142+ begin
143+ if SL.Count = 0 then
144+ raise SysUtils.EArgumentException.Create(
145+ ' RandomString called with empty string list'
146+ );
147+ Result := SL[Random(SL.Count)];
148+ end ;
149+
150+ {
151+ Returns the reverse of the given string.
152+ }
57153function ReverseStr (S: string): string;
58154begin
59155 Result := SysUtils.EmptyStr;
@@ -64,6 +160,9 @@ function ReverseStr(S: string): string;
64160 end ;
65161end ;
66162
163+ {
164+ Returns the reverse of the given string.
165+ }
67166function ReverseStrR (const S: string): string;
68167begin
69168 if SysUtils.AnsiSameText(S, SysUtils.EmptyStr) or (System.Length(S) = 1 ) then
@@ -73,21 +172,11 @@ function ReverseStrR(const S: string): string;
73172 + ReverseStrR(StrUtils.LeftStr(S, System.Length(S) - 1 ))
74173end ;
75174
76- function IsNumeric (Value : string; const AllowFloat: Boolean;
77- const TrimWhiteSpace: Boolean = True): Boolean;
78- var
79- ValueInt: Int64; // dummy integer value
80- ValueFloat: Extended; // dummy float value
81- begin
82- if TrimWhiteSpace then
83- Value := SysUtils.Trim(Value );
84- // Check for valid integer
85- Result := SysUtils.TryStrToInt64(Value , ValueInt);
86- if not Result and AllowFloat then
87- // Wasn't valid as integer, try float
88- Result := SysUtils.TryStrToFloat(Value , ValueFloat);
89- end ;
90-
175+ {
176+ Splits the string AText into segments separated by Delimiter and creates and
177+ returns a string list containing the segments.
178+ The caller is responsible for freeing the returnd string list object.
179+ }
91180function SplitString (const AText, ADelimiter: string): Classes.TStringList;
92181var
93182 LTxt, LTmp: string;
@@ -104,23 +193,15 @@ function SplitString(const AText, ADelimiter: string): Classes.TStringList;
104193 Result.Add(LTxt);
105194end ;
106195
107- procedure ParseStr (const StrToParse: string; const Delimiter: Char;
108- const Words: Classes.TStringList);
109- var
110- TmpInStr: string;
196+ {
197+ Strips all accelerator ('&') characters from the given string and returns the
198+ resulting string.
199+ }
200+ function StripAccelChars (const S: string): string;
111201begin
112- TmpInStr := StrToParse;
113- Words.Clear;
114- if Length(TmpInStr) > 0 then
115- begin
116- while Pos(Delimiter, TmpInStr) > 0 do
117- begin
118- Words.Append(Copy(TmpInStr, 1 , Pos(Delimiter, TmpInStr) - 1 ));
119- Delete(TmpInStr, 1 , Pos(Delimiter, TmpInStr));
120- end ;
121- Words.Append(TmpInStr);
122- end ;
202+ Result := SysUtils.StringReplace(
203+ S, ' &' , SysUtils.EmptyStr, [SysUtils.rfReplaceAll]
204+ );
123205end ;
124206
125207end .
126-
0 commit comments