Page 1 of 1
Ltrim -command for any character
Posted: Sat Aug 01, 2020 6:02 am
by jarkky
Can the Ltrim command remove also other character than space?
For example:
string="--fdfd--"
using command
ltrim(string,'-')
could it remove the leading '-' characters?
Re: Ltrim -command for any character
Posted: Sat Aug 01, 2020 8:29 am
by Zizi64
You can trim one or more characters by an User defined macro function. It is works inside a macro code or called it from a spreadsheet Cell:
Code: Select all
REM ***** BASIC *****
Function myLTrim(InputStr as string, StrToTrim as string) as String
If Left(InputStr, Len(StrToTrim)) = StrToTrim then
myLTrim = Right(InputStr, Len(InputStr)-Len(StrToTrim))
else
myLTrim = InputStr
end if
End function
Re: Ltrim -command for any character
Posted: Sat Aug 01, 2020 10:30 am
by JeJe
Note: Not thoroughly tested
Code: Select all
Sub Main
st = "--fdfd--"
msgbox LTrimAny (st,"-")
msgbox RTrimAny(st,"-")
msgbox TrimAny(st,"-")
End Sub
function LTrimAny(byval st,trimchar)
dim c as long,i as long
for i = 1 to len(st)
if mid(st,i,1)= trimchar then
c = c+1
else
exit for
end if
next
if c>0 then mid(st,1,c) =""
LTrimAny= st
end function
function RTrimAny(byval st,trimchar)
dim c as long,i as long,lenst
lenst= len(st)
for i = lenst to 1 step -1
if mid(st,i,1)= trimchar then
c = c+1
else
exit for
end if
next
if c>0 then mid(st,lenst -c+1,c) =""
RTrimAny= st
end function
function TrimAny(byval st,trimchar)
st=LTrimAny(st,trimchar)
TrimAny=RTrimAny(st,trimchar)
end function
Edit: added TrimAny
Re: Ltrim -command for any character
Posted: Sat Aug 01, 2020 12:45 pm
by Villeroy
Install Hubert Lambert's most useful pystring extension from
viewtopic.php?f=20&t=83856&p=389632&hil ... ng#p389632
Re: Ltrim -command for any character
Posted: Sat Aug 01, 2020 11:38 pm
by JeJe
Slightly simpler functions:
Code: Select all
Sub Main
st = "--fdfd--"
msgbox LTrimAny (st,"-")
msgbox RTrimAny(st,"-")
msgbox TrimAny(st,"-")
End Sub
function LTrimAny(byval st,trimchar)
dim i as long
for i = 1 to len(st)
if mid(st,i,1)<> trimchar then exit for
next
if i>1 then mid(st,1,i-1) =""
LTrimAny= st
end function
function RTrimAny(byval st,trimchar)
dim i as long,lenst as long
lenst = len(st)
for i = lenst to 1 step -1
if mid(st,i,1)<> trimchar then exit for
next
if i< lenst then mid(st,i+1,i-lenst) =""
RTrimAny= st
end function
function TrimAny(byval st,trimchar)
st=LTrimAny(st,trimchar)
TrimAny=RTrimAny(st,trimchar)
end function
Re: Ltrim -command for any character
Posted: Mon Aug 03, 2020 8:05 pm
by Lupp
A slghtly different approach.
What shall be trimmed away may consist of more than one character, and -if letters occur- there is a choice to make the functions case insensitive by replacing a 0 by a 1.
Code: Select all
Function withoutJunkLeft( pString As String, pJunk As String)
l = Len(pJunk)
pos = 1
While InStr(pos, pString, pJunk, 0)=pos
pos = pos + l
Wend
withoutJunkLeft = Mid(pString, pos, 65535)
End Function
Function withoutJunkRight( pString As String, pJunk As String)
l = Len(pJunk)
pos = Len(pString) - l + 1
While InStr(pos, pString, pJunk, 0)=pos
pos = pos - l
Wend
withoutJunkRight = Left(pString, pos+l-1)
End Function
Re: Ltrim -command for any character
Posted: Tue Aug 04, 2020 12:38 am
by JeJe
Other versions of LTrim allow any of a list of characters, not just a single one, or a repeated string.
Code: Select all
Sub Main
st = "-q-fdfd--"
msgbox LTrimAny (st,"f-pq")
msgbox RTrimAny(st,"p-")
msgbox TrimAny(st,"-q")
End Sub
function LTrimAny(byval st,trimchars)
dim i as long
for i = 1 to len(st)
if instr(1, trimchars, mid(st,i,1)) = 0 then exit for
next
if i>1 then mid(st,1,i-1) =""
LTrimAny= st
end function
function RTrimAny(byval st,trimchars)
dim i as long,lenst as long
lenst = len(st)
for i = lenst to 1 step -1
if instr(1, trimchars, mid(st,i,1)) = 0 then exit for
next
if i< lenst then mid(st,i+1,i-lenst) =""
RTrimAny= st
end function
function TrimAny(byval st,trimchars)
st=LTrimAny(st,trimchars)
TrimAny=RTrimAny(st,trimchars)
end function