Ltrim -command for any character

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
jarkky
Posts: 14
Joined: Sat Oct 12, 2019 5:18 pm

Ltrim -command for any character

Post 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?
OpenOffice 3.1 on CentOS Linux release 8.0.1905 (Core)
User avatar
Zizi64
Volunteer
Posts: 11359
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Ltrim -command for any character

Post 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
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.
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: Ltrim -command for any character

Post 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
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Ltrim -command for any character

Post by Villeroy »

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
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: Ltrim -command for any character

Post 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

Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Lupp
Volunteer
Posts: 3548
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Ltrim -command for any character

Post 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 
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: Ltrim -command for any character

Post 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


Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply