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

Tobias Boege taboege at gmail.com
Mon Nov 9 15:29:36 CET 2020


On Mon, 09 Nov 2020, Bruce Steers wrote:
> Is there a function like this for cleaning up a string of erroneous spaces
> ..
> 
> *Public Sub InTrim(sVar As String) As StringDim sOld, sNew, sChar, sLast As
> String, iPos As IntegersOld = Trim(sVar)sNew = ""For iPos = 0 To sVar.Len -
> 1 sChar = sVar[iPos, 1] If IsSpace(sChar) And IsSpace(sLast) Then
> Continue sNew &= sChar sLast = sCharNextReturn Trim(sNew)End*
> 
> That will turn " This    has    many        spaces  "
> into "This has many spaces"
> 

No, but if you don't mind loading gb.pcre, it can be as short as

  Trim$(RegExp.Replace(sVar, "[[:space:]]+", " ", RegExp.Greedy))

Best,
Tobias

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk


More information about the User mailing list