[Gambas-user] trimming extra spaces from within a string.

Bruce Steers bsteers4 at gmail.com
Mon Nov 9 19:38:04 CET 2020


do you think using Replace() would be faster than my initial InTrim()
function method ?
Cheers
Bruce

On Mon, 9 Nov 2020 at 17:31, Christof Thalhofer <chrisml at deganius.de> wrote:

> Am 09.11.20 um 17:46 schrieb KKing:
> > how about
> > strInput = " This    has    many        spaces  "
> > strResult = Trim(Replace(strInput,"  "," "))
> >
> >> That will turn " This    has    many        spaces  "
> >> into "This has many spaces"
>
> Nope. I wrote a test:
>
> > Test TDegString:TrimMult
> >   not ok 1 -
> >   #
> >   # ------------- Expected -------------
> >   # This has many spaces
> >   #
> >   # ---------------- Got ---------------
> >   # This  has  many    spaces
> >   # ------------------------------------
> >   #
> >   # Strings are of different lengths 25 and 20, respectively.
> >   # Strings differ at position 6.
>
> But this works:
>
> --------------------------------------------------
>   While InStr(Text, "  ")
>       Text = Replace(Text, "  ", " ")
>   Wend
>
>   Return Trim(Text)
> --------------------------------------------------
>
> And it's about three times faster than Tobi's idea.
>
> Alles Gute
>
> Christof Thalhofer
>
> --
> Dies ist keine Signatur
>
>
> ----[ http://gambaswiki.org/wiki/doc/netiquette ]----
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20201109/98b94484/attachment.htm>


More information about the User mailing list