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.
'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
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"
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
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
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
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 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
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
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.
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?
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
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