Can the Ltrim command remove also other character than space?
For example:
string="--fdfd--"
using command
ltrim(string,'-')
could it remove the leading '-' characters?
Ltrim -command for any character
Ltrim -command for any character
OpenOffice 3.1 on CentOS Linux release 8.0.1905 (Core)
Re: Ltrim -command for any character
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
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
Re: Ltrim -command for any character
Note: Not thoroughly tested
Edit: added TrimAny
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
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Re: Ltrim -command for any character
Install Hubert Lambert's most useful pystring extension from viewtopic.php?f=20&t=83856&p=389632&hil ... ng#p389632
Code: Select all
=PYSTRING("lstrip";A1;"-")
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Re: Ltrim -command for any character
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
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Re: Ltrim -command for any character
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.
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
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
---
Lupp from München
Re: Ltrim -command for any character
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
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)