A sub for trimming alphabet in a string

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
Takase
Posts: 13
Joined: Feb 09, 2018 14:12

A sub for trimming alphabet in a string

Post by Takase »

I found a topic about trimming characters in a string in the forum before. But I've lost the link to the topic and I'm lazy to find it again. So, I wrote myself a little piece of code to trim a specified alphabet from a string.
It is basically "trim()" but it runs through the string to trim all the matching alphabet in the string instead of only trimming the matching alphabet at the leftmost side and the rightmost side of the string. I hope this will help somebody.

Note: Edited a little bit. Instead of Select case, if thenis used.

trim.bi

Code: Select all

'a low performance trimmer(maybe?)
#pragma once

'a simple lookup table for ASCII characters
dim shared as string trimset(255)
for i as short = 0 to 255
	trimset(i) = chr(i)
next

sub trimInStr(instring as string, byref outstring as string, trimchar as string)
	'clears outstring first
	outstring = ""
	'compare the string: if instring <> trimchar then copy the currently compared
	'character to the string
	for i as long = 0 to len(instring)-1
		if instring[i] <> trimchar[0] then : outstring += trimset(instring[i]) : end if
	next
end sub
Here's an example:

Code: Select all

#include "trim.bi"

dim as string outputstr
trimInStr("this is a test", outputstr, " ") ' trims spaces
print outputstr 'should print: "thisisatest"
Last edited by Takase on Feb 19, 2018 5:23, edited 1 time in total.
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: A sub for trimming alphabet in a string

Post by badidea »

Looks more complicated than it needs to be. Why the trimset and select case?
Same functionality:

Code: Select all

sub trimInStr(instring as string, byref outstring as string, trimchar as string)
	outstring = ""
	for i as long = 0 to len(instring)-1
		if instring[i] <> trimchar[0] then outstring += chr(instring[i])
	next
end sub

dim as string outputstr
trimInStr("this is a test", outputstr, " ") ' trims spaces
print outputstr 'should print: "thisisatest"
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: A sub for trimming alphabet in a string

Post by dodicat »

outstring+=chr(instring)
is speeded up by the lookup table trimset.
(I assume this, I haven't tested)
but I agree, select case is not needed.
Here is another variety

Code: Select all

Function trimInStr(Byval TextIn As String,Char As String) As String
    dim as long ii,AC=Asc(char)
    For i As Long = 0 To Len(Textin)-1
        If Textin[i]<>AC Then Textin[ii]=Textin[i]:ii +=1
    Next 
    Return Left(TextIn,ii)
End Function

dim as string outputstr
outputstr= trimInStr("this is a test"," ") ' trims spaces
print outputstr 'should print: "thisisatest"
sleep

MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: A sub for trimming alphabet in a string

Post by MrSwiss »

Even simpler: Function instead of Sub (direct result):

Code: Select all

Function trimInStr( _
    ByRef instring As Const String, _
    ByRef trimchar As Const String _
    ) As String
   Dim As String outstring = ""

   For i As UInteger = 0 To Len(instring) - 1   ' fastest up-counter (32/64)
      If instring[i] <> trimchar[0] Then outstring += Chr(instring[i])
   Next

   Function = outstring
End Function

' removes the stars ...
Print trimInStr("*greetings from *FreeBASIC*!", "*")
Sleep
sancho3
Posts: 358
Joined: Sep 30, 2017 3:22

Re: A sub for trimming alphabet in a string

Post by sancho3 »

Mr.Swiss version with a few changes to make it even faster:

Code: Select all

Function trimInStr( _
	ByRef instring As Const String, _
	ByRef trimchar As Const String _
		) As String
	dim as integer l = len(instring) - 1, n
	Dim As String outstring = space(l + 1)

	For i As UInteger = 0 To l
		If instring[i] <> trimchar[0] Then 
			n += 1
			outstring[n] = instring[i]
		endif 
	Next

   Function = trim(outstring)
End Function
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: A sub for trimming alphabet in a string

Post by dodicat »

From my version 3 posts ago, compare:

Code: Select all


Function trimInStr( _
   ByRef instring As Const String, _
   ByRef trimchar As Const String _
      ) As String
   dim as integer l = len(instring) - 1, n
   Dim As String outstring = space(l + 1)

   For i As UInteger = 0 To l
      If instring[i] <> trimchar[0] Then 
         n += 1
         outstring[n] = instring[i]
      endif 
   Next

   Function = trim(outstring)
End Function

Function trimInStr2(Byval TextIn As String,Char As String) As String
    dim as long ii,AC=Asc(char)
    For i As Long = 0 To Len(Textin)-1
        If Textin[i]<>AC Then Textin[ii]=Textin[i]:ii +=1
    Next 
    Return Left(TextIn,ii)
End Function


dim shared as string s:s=string(10000000,0)
for n as long=1 to 10000000
    s[n]=48+rnd*10
    if n mod 2=0 then s[n]=32
next
dim as string g=s
dim as long L
dim as double t
t=timer
 L=len(trimInstr(s," "))
print timer-t
print L
print



t=timer
 L=len(trimInstr2(g," "))
print timer-t
print L



sleep
 
sancho3
Posts: 358
Joined: Sep 30, 2017 3:22

Re: A sub for trimming alphabet in a string

Post by sancho3 »

dodicat wrote:From my version 3 posts ago, compare:
Sorry dodicat, I didn't even pay attention to your version.
Its very similar to what I posted but a little faster.
Takase
Posts: 13
Joined: Feb 09, 2018 14:12

Re: A sub for trimming alphabet in a string

Post by Takase »

dodicat wrote:outstring+=chr(instring)
is speeded up by the lookup table trimset.
(I assume this, I haven't tested)
but I agree, select case is not needed.
Here is another variety

Code: Select all

Function trimInStr(Byval TextIn As String,Char As String) As String
    dim as long ii,AC=Asc(char)
    For i As Long = 0 To Len(Textin)-1
        If Textin[i]<>AC Then Textin[ii]=Textin[i]:ii +=1
    Next 
    Return Left(TextIn,ii)
End Function

dim as string outputstr
outputstr= trimInStr("this is a test"," ") ' trims spaces
print outputstr 'should print: "thisisatest"
sleep

yes. Trimset is for speeding up the overall speed as calling chr() everytime seems to slow down the program.
Takase
Posts: 13
Joined: Feb 09, 2018 14:12

Re: A sub for trimming alphabet in a string

Post by Takase »

About the Select case , its my habit to use it instead of if else. Looks like my habit had got me. Thanks for all the replies tho! BTW, I did not do any DIMs in the sub because I'm afraid that it'll slow the sub down. So, can anyone tell me if DIMming the variables every call will decrease the performance or not? Will it bring a huge impact to the program speed?
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: A sub for trimming alphabet in a string

Post by dodicat »

You have some choices for local variables.

Code: Select all


sub dothis
    dim as long a,b,c,d,e,f,g,h,i
    d=13
end sub

sub dothis2
    static as long a,b,c,d,e,f,g,h,i
    d=13
end sub


sub dothis3
    dim as long a=any
    dim as long b=any
    dim as long c=any
    dim as long d=any
    dim as long e=any
    dim as long f=any
    dim as long g=any
    dim as long h=any
    dim as long i=any
    d=13
end sub

dim as double t

t=timer
for n as long=1 to 1000000
    dothis
    next
    print timer-t
    
    
    print
    t=timer
for n as long=1 to 1000000
    dothis2
    next
    print timer-t
    
    print
     t=timer
for n as long=1 to 1000000
    dothis3
    next
    print timer-t
    
    sleep
     
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: A sub for trimming alphabet in a string

Post by Tourist Trap »

Takase wrote:I found a topic about trimming characters in a string in the forum before. But I've lost the link to the topic and I'm lazy to find it again. So, I wrote myself a little piece of code to trim a specified alphabet from a string.
yes there was a nice topic focused on performance in the General section, but I dont remember who started it. I guess you re talking about this one. From my part I did some general purpose stuff here below, dont know if it can help. No performance is targetted there:
viewtopic.php?f=7&t=25299&p=227595#p227595
Post Reply