@@ -242,9 +242,9 @@ end function
242242' ----------------------------------------------------------------
243243' function ltrim(ByVal s$, rep$)
244244'
245- ' Returns a copy of b $, removing all occurrences of rep$ on the
245+ ' Returns a copy of s $, removing all occurrences of rep$ on the
246246' left side (beginning) of s$. For example:
247- '
247+ ' ltrim(": Hello world", ": ") returns "Hello World"
248248' ----------------------------------------------------------------
249249function ltrim( ByVal s$, ByVal rep$) as String
250250 DIM i as Uinteger = 0
@@ -264,6 +264,47 @@ function ltrim(ByVal s$, ByVal rep$) as String
264264end function
265265
266266
267+
268+ ' ----------------------------------------------------------------
269+ ' function rtrim(ByVal s$, rep$)
270+ '
271+ ' Returns a copy of s$, removing all occurrences of rep$ on the
272+ ' right side (ending) of s$. For example:
273+ ' rtrim("Hello world. ", ". ") returns "Hello World"
274+ ' ----------------------------------------------------------------
275+ function rtrim( ByVal s$, ByVal rep$) as String
276+ DIM i as Integer
277+ DIM d, l2 as Uinteger
278+ DIM l as Uinteger = len(rep$)
279+
280+ l2 = len(s$)
281+ if not l or l2 < l then return s$
282+
283+ d = l - 1
284+ i = l2 - 1
285+
286+ while i >= d and rep = s(i - d to i)
287+ i = i - l
288+ end while
289+
290+ if i < 0 then return ""
291+
292+ return s$( to i)
293+ end function
294+
295+
296+
297+ ' ----------------------------------------------------------------
298+ ' function trim(ByVal s$, rep$)
299+ '
300+ ' Returns a copy of s$, removing all occurrences of rep$ on the
301+ ' left and right side (ending) of s$. For example:
302+ ' rtrim(";.;.Hello world;.;.", ";.") returns "Hello World"
303+ ' ----------------------------------------------------------------
304+ function trim( ByVal s$, ByVal rep$) as String
305+ return ltrim(rtrim(s$, rep$), rep$)
306+ end function
307+
267308#undef __MAX_LEN__
268309
269310#pragma pop(string_base)
0 commit comments