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

Martin mbelmonte at belmotek.net
Tue Nov 10 11:40:28 CET 2020


Hi Rolf, i think in the function that you propose don't remove the 
spaces at the beginning and at the end of the string.

Anyway I think the good function is the Christof proposal because is 
very simple and everybody can manage that "technology". I say the good 
not the best that is Tobias proposal using regex in my computer at least 
:-).

Regards.

On 10/11/20 10:51, Rolf-Werner Eilert wrote:
> 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
>
> ----[ http://gambaswiki.org/wiki/doc/netiquette ]----


More information about the User mailing list