-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringTools.pas
181 lines (152 loc) · 4.12 KB
/
StringTools.pas
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
unit StringTools;
interface
uses
windows, sysutils;
type
Texplodedstr = array of string;
function ParseBracket(Text: string; const _open: char = '('; const _close: char = ')'): string;
function replaceword(const s: string; NewWord: string; wordIndex: integer; karakter: char): string;
function explodestr(const s: string; wordIndex: integer; karakter: char): string;
function SearchAndReplace(sSrc, sLookFor, sReplaceWith: string): string;
function charcount(str: string; chr: string): integer;
function Removelastchar(s: string): string;
function Randomstring(strLen: integer): string;
function _explodestr(str: string; delim: char): Texplodedstr;
function SafeString(const Input: string): string;
implementation
const
Alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
function SafeString(const Input: string): string;
var
i: Integer;
Output: string;
begin
Output := '';
for i := 1 to Length(Input) do
begin
case Input[i] of
#8:
Output := Output + '\b'; // Backspace
#9:
Output := Output + '\t'; // Horizontal Tab
#10:
Output := Output + '\n'; // Line Feed
#12:
Output := Output + '\f'; // Form Feed
#13:
Output := Output + '\r'; // Carriage Return
#34:
Output := Output + '\"'; // Double Quote
#39:
Output := Output + '\' + #39; // Single Quote
#92:
Output := Output + '\\'; // Backslash
else
Output := Output + Input[i];
end;
end;
Result := Output;
end;
function _explodestr(str: string; delim: char): Texplodedstr;
var
i: integer;
count: integer;
begin
count := charcount(str, delim);
setlength(result, count + 1);
for i := 0 to count do
begin
result[i] := explodestr(str, i, delim);
end;
end;
function charcount(str: string; chr: string): integer;
var
say: integer;
i: integer;
begin
say := 0;
for i := 1 to length(str) do
begin
if str[i] = chr then
inc(say);
end;
result := say;
end;
function Randomstring(strLen: integer): string;
begin
Randomize;
Result := '';
repeat
Result := Result + Alphabet[Random(Length(Alphabet)) + 1];
until (Length(Result) = strLen)
end;
function SearchAndReplace(sSrc, sLookFor, sReplaceWith: string): string;
var
nPos, nLenLookFor: integer;
begin
nPos := Pos(sLookFor, sSrc);
nLenLookFor := Length(sLookFor);
while (nPos > 0) do
begin
Delete(sSrc, nPos, nLenLookFor);
Insert(sReplaceWith, sSrc, nPos);
nPos := Pos(sLookFor, sSrc);
end;
Result := sSrc;
end;
function Removelastchar(s: string): string;
begin
Result := copy(s, 1, Length(s) - 1);
end;
function replaceword(const s: string; NewWord: string; wordIndex: integer; karakter: char): string;
var
tmp: string;
i: integer;
begin
for i := 0 to charcount(s, karakter) do
begin
if i = wordIndex then
tmp := tmp + NewWord
else
tmp := tmp + explodestr(s, i, karakter) + karakter;
end;
if AnsiLastChar(tmp) = karakter then
tmp := Removelastchar(tmp);
Result := tmp;
end;
function explodestr(const s: string; wordIndex: integer; karakter: char): string;
var
index, counter: integer;
begin
Result := trim(s);
counter := 0;
index := Pos(karakter + karakter, Result);
while index > 0 do
begin
Delete(Result, index, 1);
index := Pos(karakter + karakter, Result);
end;
index := Pos(karakter, Result);
while ((counter < wordIndex) and (index > 0)) do
begin
Delete(Result, 1, index);
index := Pos(karakter, Result);
counter := counter + 1;
end;
if (counter < wordIndex) then
Result := '';
index := Pos(karakter, Result);
if index > 0 then
Delete(Result, index, maxInt);
end;
function ParseBracket(Text: string; const _open: char = '('; const _close: char = ')'): string;
var
pos1, pos2: integer;
begin
Result := '';
pos1 := Pos(_open, Text);
pos2 := Pos(_close, Text);
if (pos1 > 0) and (pos2 > pos1) then
Result := copy(Text, pos1 + 1, pos2 - pos1 - 1);
end;
end.