1
1
{
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.
6
14
}
7
15
8
16
unit UStringCatSnippets;
9
17
10
18
{ $IFNDEF FPC}
11
19
{ $IFDEF CONDITIONALEXPRESSIONS}
20
+ { $IF CompilerVersion >= 24.00}
21
+ { $LEGACYIFEND ON}
22
+ { $IFEND}
12
23
{ $IF CompilerVersion >= 14.00}
13
24
{ $WARN SYMBOL_PLATFORM OFF}
14
25
{ $WARN SYMBOL_DEPRECATED OFF}
29
40
interface
30
41
31
42
uses
32
- SysUtils, StrUtils, Classes, Windows ;
43
+ SysUtils, Classes, StrUtils ;
33
44
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);
35
61
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
+ }
36
71
function ReverseStr (S: string): string;
37
72
73
+ {
74
+ Returns the reverse of the given string.
75
+ }
38
76
function ReverseStrR (const S: string): string;
39
77
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
+ }
43
83
function SplitString (const AText, ADelimiter: string): Classes.TStringList;
44
84
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;
47
90
48
91
implementation
49
92
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
51
105
begin
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 ;
55
135
end ;
56
136
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
+ }
57
153
function ReverseStr (S: string): string;
58
154
begin
59
155
Result := SysUtils.EmptyStr;
@@ -64,6 +160,9 @@ function ReverseStr(S: string): string;
64
160
end ;
65
161
end ;
66
162
163
+ {
164
+ Returns the reverse of the given string.
165
+ }
67
166
function ReverseStrR (const S: string): string;
68
167
begin
69
168
if SysUtils.AnsiSameText(S, SysUtils.EmptyStr) or (System.Length(S) = 1 ) then
@@ -73,21 +172,11 @@ function ReverseStrR(const S: string): string;
73
172
+ ReverseStrR(StrUtils.LeftStr(S, System.Length(S) - 1 ))
74
173
end ;
75
174
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
+ }
91
180
function SplitString (const AText, ADelimiter: string): Classes.TStringList;
92
181
var
93
182
LTxt, LTmp: string;
@@ -104,23 +193,15 @@ function SplitString(const AText, ADelimiter: string): Classes.TStringList;
104
193
Result.Add(LTxt);
105
194
end ;
106
195
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;
111
201
begin
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
+ );
123
205
end ;
124
206
125
207
end .
126
-
0 commit comments