Trim Off Trailing Backslash or Returns from a File Path / Folder Path

Mindwatering Incorporated

Author: Tripp W Black

Created: 09/12/2012 at 09:24 PM

 

Category:
Notes Developer Tips
LotusScript

Issue:
Need to trim off a trailing/ending forward or back-slash off a string containing a folder path.

Solution:
Function TrimTrailingPathSlash(sourcestring as String, slashchar As String) as String
Dim i as Integer ' get length for trim
Dim lastchar as String ' last character to check if slashchar
i% = Len(sourcestring)
lastchar - Mid(sourcestring, i%, 1)
If (lastchar = slashchar) Then
' trim it off
TrimTrailingPathSlash = Mid(sourceString, 1, i-1)
Else
' return original
TrimTrailingPathSlash = sourceString
End If
End Function

Function RemoveTrailingSlash(pathstr As String) As String
' removes trailing backslash, returns original string if not found or error
Dim index as Integer
Dim lastchar as String ' last character to see if /

On Error Goto FErrorHandler

If (pathstr="") Then
RemoveTrailingSlash = ""
Exit Function
End If
' start w/result being original string
RemoveTrailingSlash = pathstr
index = Len(pathstr)
If (index < 1) Then
Exit Function
End If
lastchar = Right$(pathstr, 1)
If (lastchar = |\|) Then
' remove it
RemoveTrailingSlash = Left$(pathstr, index-1)
Else
RemoveTrailingSlash = pathstr
End If
FExit:
Exit Function
FErrorHander:
' general occurs for o/s access issues
RemoveTrailingSlash = pathstr
Resume FExit
End Function

Issue:
Need to trim off a carriage / line returns (10 and 13) from multi-line / multivalue strings.

Solution:
Function TrimReturns(sourcestr as String, replacechar as String) as String
' removes UNIX and WINDOWS line returns from original string
Dim returnstr as String ' returned trimmed string

On Error Goto FErrorHandler

If (sourcestr ="") Then
TrimReturns= ""
Exit Function
End If

' start w/result being original string
TrimReturns= sourcestr

' trim out 13
returnstr = Replace(sourcestr, Chr(13), replacechar)
' trim out 10
returnstr = Replace(returnstr, Chr(10), replacechar)

' return trimmed string
TrimReturns= returnstr
FExit:
Exit Function

FErrorHander:
' return starting string
TrimReturns= sourcestr
Resume FExit
End Function


previous page