Ltrim -command for any character

Creating a macro - Writing a Script - Using the API

Ltrim -command for any character

Postby jarkky » Sat Aug 01, 2020 6:02 am

Can the Ltrim command remove also other character than space?
For example:
string="--fdfd--"
using command
ltrim(string,'-')
could it remove the leading '-' characters?
OpenOffice 3.1 on CentOS Linux release 8.0.1905 (Core)
jarkky
 
Posts: 10
Joined: Sat Oct 12, 2019 5:18 pm

Re: Ltrim -command for any character

Postby Zizi64 » Sat Aug 01, 2020 8:29 am

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   Expand viewCollapse view
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; LO6.1.6, 6.2.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-6.4.5;AOO4.1.7
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.
User avatar
Zizi64
Volunteer
 
Posts: 9653
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Ltrim -command for any character

Postby JeJe » Sat Aug 01, 2020 10:30 am

Note: Not thoroughly tested

Code: Select all   Expand viewCollapse view
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
Openoffice 4.1.6
Windows 8
JeJe
Volunteer
 
Posts: 1246
Joined: Wed Mar 09, 2016 2:40 pm

Re: Ltrim -command for any character

Postby Villeroy » Sat Aug 01, 2020 12:45 pm

Install Hubert Lambert's most useful pystring extension from viewtopic.php?f=20&t=83856&p=389632&hilit=pystring#p389632

Code: Select all   Expand viewCollapse view
=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, no OpenOffice, LibreOffice 6.4
User avatar
Villeroy
Volunteer
 
Posts: 28842
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Ltrim -command for any character

Postby JeJe » Sat Aug 01, 2020 11:38 pm

Slightly simpler functions:

Code: Select all   Expand viewCollapse view
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

Openoffice 4.1.6
Windows 8
JeJe
Volunteer
 
Posts: 1246
Joined: Wed Mar 09, 2016 2:40 pm

Re: Ltrim -command for any character

Postby Lupp » Mon Aug 03, 2020 8:05 pm

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   Expand viewCollapse view
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 7.0 and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
User avatar
Lupp
Volunteer
 
Posts: 2938
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Ltrim -command for any character

Postby JeJe » Tue Aug 04, 2020 12:38 am

Other versions of LTrim allow any of a list of characters, not just a single one, or a repeated string.

Code: Select all   Expand viewCollapse view
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


Openoffice 4.1.6
Windows 8
JeJe
Volunteer
 
Posts: 1246
Joined: Wed Mar 09, 2016 2:40 pm


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 8 guests