Menu

[4a4584]: / 3rdParty / StrUtilsD9.pas  Maximize  Restore  History

Download this file

77 lines (64 with data), 2.0 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Implementation of needed Helpers for Delphi < XE
unit StrUtilsD9;
interface
uses
Types;
function SplitString(const S, Delimiters: string): TStringDynArray;
implementation
uses
Generics.Collections,
StrUtils;
function SplitString(const S, Delimiters: string): TStringDynArray;
var
delimiterIndex: Integer;
currentDelimiter: Char;
delimiterPositions: TList<Integer>;
currentDelimiterPosition, nextDelimiterPosition: Integer;
i: Integer;
begin
{$IF CompilerVersion >= 21}
Result := StrUtils.SplitString(S, Delimiters);
Exit;
{$IFEND}
SetLength(Result, 0);
delimiterPositions := TList<Integer>.Create;
try
for delimiterIndex := 1 to Length(Delimiters) do
begin
currentDelimiter := Delimiters[delimiterIndex];
currentDelimiterPosition := Pos(currentDelimiter, S);
while currentDelimiterPosition > 0 do
begin
if not delimiterPositions.Contains(currentDelimiterPosition) then
delimiterPositions.Add(currentDelimiterPosition);
currentDelimiterPosition := PosEx(currentDelimiter, S, currentDelimiterPosition + 1);
end;
end;
if delimiterPositions.Count = 0 then
begin
SetLength(Result, 1);
Result[0] := S;
end
else
begin
SetLength(Result, delimiterPositions.Count + 1);
delimiterPositions.Sort;
for i := 0 to delimiterPositions.Count do
begin
if i = 0 then
currentDelimiterPosition := 0
else
currentDelimiterPosition := delimiterPositions[i - 1];
if i = delimiterPositions.Count then
nextDelimiterPosition := Length(S) + 1
else
nextDelimiterPosition := delimiterPositions[i];
Result[i] := Copy(S, currentDelimiterPosition + 1, nextDelimiterPosition - currentDelimiterPosition - 1);
end;
end;
finally
delimiterPositions.Clear;
delimiterPositions.Free;
end;
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.