[Gambas-user] trimming extra spaces from within a string.
Rolf-Werner Eilert
rwe-sse at osnanet.de
Tue Nov 10 10:51:48 CET 2020
Am 10.11.20 um 10:33 schrieb Christof Thalhofer:
> Am 10.11.20 um 10:08 schrieb Martin:
>
>> Hi, i made some tests also.
>>
>> the results was:
>>
>> ' The faster
>>
>> Public Function BrianTrim(sRaw As String) As String
>
> This should be called TobiTrim, as this idea came from Tobi:
>
>> Dim sOut As String
>> sOut = Trim$(RegExp.Replace(sRaw, " {2,}+", " "))
>> Return sOut
>> End
>
> Very funny, on my computer Regexp is significantly slower, maybe it is
> also a matter of computational power or even cpu cores?
>
The regexp thing looks really cool, though I would never be able to make
it myself :)
Now, at the beginning of this discussion I had this idea. It's not that
elegant, it only uses low-level functions, and it uses some more
variables, but it runs at least:
Private Function RolfTrim(sRaw As String) As String
Dim sOut, sChr As String
Dim po, ctr As Integer
For po = 1 To String.Len(sRaw)
sChr = String.Mid(sRaw, po, 1)
If sChr = " " Then
If ctr = 0 Then
ctr = 1
sOut &= " "
Endif
Else
ctr = 0
sOut &= sChr
Endif
Next
Return sOut
End
One might want to replace ctr by a Boolean and ask for True and False
instead.
Regards
Rolf
More information about the User
mailing list