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

Christof Thalhofer chrisml at deganius.de
Mon Nov 9 18:30:51 CET 2020


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

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: OpenPGP digital signature
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20201109/92351eb9/attachment-0001.sig>


More information about the User mailing list