LotusScript Sample Code to Left Trim and Trim a String at the End of a Number and Modify It

Mindwatering Incorporated

Author: Tripp W Black

Created: 07/11/2003 at 11:20 AM

 

Category:
Notes Developer Tips
LotusScript

The attached script trims a string to "n" charcters and optionally trims any whitespace around the string.
DoLeftTrim_Function.txtDoLeftTrim_Function.txt


This agent is a sample script that takes a string (e.g. 7890 XX10) and modifies it to a standard format (e.g. 000786712 XX10).


Function TrimNumStr(oldstring As String, pads As Integer) As String
' this function trims numbers in a string to the first non number working left to right, and
' then pads it to a certain number of characters (requires PadString function)
' oldstring = string to manipulate, pads = number of characters to pad

' trimming dims
Dim n As Integer ' x counting variable for a for loop
Dim x As Integer ' n current character being evaluated
Dim LowerLimit As Integer ' ascii code for 0
Dim UpperLimit As Integer ' ascii code for 9
LowerLimit = 48 ' ascii code for number 0
UpperLimit = 57 ' ascii code for number 9
Dim newstring As String ' new string trimmed & padded
Dim stringsuffix As String ' suffix ending to add back after padding

' initialize working variables
x = 0
n = 0

' if we are passed nothing in old string parameters
If oldstring = "" Then
TrimNumStr = oldstring
Exit Function
End If

'x is just an integer pointer (it also tells us where to trim)
For x = 1 To Len(oldstring)
n = Asc(Mid$(oldstring,x,1))
If n => LowerLimit And n <= UpperLimit Then
' this is a number we need to continue
Else
' this is a letter and we need to trim and pad
newstring = Left(oldstring, (x-1))
' trim it to last numerical character
stringsuffix = Right(oldstring, (Len(oldstring) - (x-1))) ' get suffix to add back after padding
newstring = PadString(newstring, pads) ' pad it
stringsuffix = ReplaceChar(stringsuffix, " ", " ") ' trim out any double spaces (char 32) with just one
TrimNumStr = newstring & stringsuffix
Exit Function
End If
Next x
' if we get here there are no characters. we need to just pad
newstring = PadString(oldstring, pads) ' pad it
TrimNumStr = newstring
Exit Function
End Function



Pad a Number with Extra Zeros:
Function PadString(origstring As String, pads As Integer)
' this function pads/adds leading 0s to a number string
Dim padding As String ' string of zeros used for padding
padding = "00000000000000000000000000000000000000000"
If Len(pads) = 0 Then
' return original string as error
PadString = origstring
End If
PadString = Right(padding & origstring, pads)
End Function


@Formula to Remove the Leading Zeros from a String:
incomingStr := "00120dn30";
@While(@Left(incomingStr ; 1) = "0";
incomingStr := @Right(incomingStr ; "0"));
incomingStr;


previous page