I think most of us have a function similar to Allen Browne’s
Function TrailingSlash(varIn As Variant) As String
If Len(varIn) > 0 Then
If Right(varIn, 1) = "\" Then
TrailingSlash = varIn
Else
TrailingSlash = varIn & "\"
End If
End If
End Function
to add a trailing slash as required to a string.
Today, I propose 2 extensions on that original function:
String_TrailingChr
This function is similar to the original, but instead of just adding a slash, we input the character to be used making it a little more universal.
'---------------------------------------------------------------------------------------
' Procedure : String_TrailingChr
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website : http://www.cardaconsultants.com
' Purpose : Ensure a string ends with the specified character
' Especially useful when working with file paths
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
' (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: None required
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' vInput : The string to ensure ends with the specified characters
' sTrailingChr : The character it needs to end with
'
' Usage:
' ~~~~~~
' ? String_TrailingChr("C:", "\")
' Returns -> C:\
'
' ? String_TrailingChr("C:\Users\Dev\", "\")
' Returns -> C:\Users\Dev\
'
' Revision History:
' Rev Date(yyyy-mm-dd) Description
' **************************************************************************************
' 1 2003-11-13
' 2 2023-02-27
'---------------------------------------------------------------------------------------
Function String_TrailingChr(vInput As Variant, sTrailingChr As String) As String
On Error GoTo Error_Handler
If IsNull(vInput) = False Then
If Len(vInput) > 0 Then
If Right(vInput, 1) = sTrailingChr Then
String_TrailingChr = vInput
Else
String_TrailingChr = vInput & sTrailingChr
End If
End If
End If
Error_Handler_Exit:
On Error Resume Next
Exit Function
Error_Handler:
MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
"Error Source: String_TrailingChr" & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Description: " & Err.Description & _
Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
, vbOKOnly + vbCritical, "An Error has Occurred!"
Resume Error_Handler_Exit
End Function
and it can be used like:
? String_TrailingChr("C:", "\")
which returns
C:\
Or
? String_TrailingChr("C:\Users\Dev\", "\")
which returns
C:\Users\Dev\
String_TrailingString
What about if we wanted a trailing string, not just a single character? Well, I have a function for that too!
Furthermore, this function is bidirectional, in the sense that it can ensure the string ends with the trailing string, or can ensure it does not (truncate it if necessary).
'---------------------------------------------------------------------------------------
' Procedure : String_TrailingString
' Author : Daniel Pineault, CARDA Consultants Inc.
' Website : http://www.cardaconsultants.com
' Purpose : Append or Remove the specified string from the input string
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
' (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
' Req'd Refs: None required
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' vInput : The string to add/remove the trailing string from
' sTrailingString : The trailing string to ensure is present, or not
' bAddTrailingString: True => Ensure the trailing string is present
' False => Ensure the trailing string is NOT present
'
' Usage:
' ~~~~~~
' ? String_TrailingString("C:\Users\Dev", "\")
' Returns -> C:\Users\Dev\
'
' ? String_TrailingString("C:\Users\Dev\", "\", False)
' Returns -> C:\Users\Dev
'
' ? String_TrailingString("C:\Users\Dev\", "\\")
' Returns -> C:\Users\Dev\\
'
' Revision History:
' Rev Date(yyyy-mm-dd) Description
' **************************************************************************************
' 1 2003-11-13
' 2 2023-02-27
'---------------------------------------------------------------------------------------
Function String_TrailingString(vInput As Variant, _
sTrailingString As String, _
Optional bAddTrailingString As Boolean = True) As String
On Error GoTo Error_Handler
Dim i As Long
Dim iNoChrs As Long
If IsNull(vInput) = False Then
If Len(vInput) > 0 Then
If Right(vInput, Len(sTrailingString)) = sTrailingString Then
If bAddTrailingString Then
String_TrailingString = vInput
Else
String_TrailingString = Left(vInput, Len(vInput) - Len(sTrailingString))
End If
Else
If bAddTrailingString Then
For i = Len(sTrailingString) To 1 Step -1
If Right(vInput, i) = Left(sTrailingString, i) Then iNoChrs = i
Next i
If iNoChrs <> 0 Then
String_TrailingString = vInput & _
Right(sTrailingString, Len(sTrailingString) - iNoChrs)
Else
String_TrailingString = vInput & sTrailingString
End If
Else
String_TrailingString = vInput
End If
End If
End If
End If
Error_Handler_Exit:
On Error Resume Next
Exit Function
Error_Handler:
MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
"Error Source: String_TrailingString" & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Description: " & Err.Description & _
Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
, vbOKOnly + vbCritical, "An Error has Occurred!"
Resume Error_Handler_Exit
End Function
and it can be used like:
? String_TrailingString("C:\Users\Dev", "\")
which returns
C:\Users\Dev\
OR
? String_TrailingString("C:\Users\Dev\", "\", False)
which returns
C:\Users\Dev
OR
? String_TrailingString("C:\Users\Dev\", "\\")
which returns
C:\Users\Dev\\
In reality, the second function is all you need since it can accomplish the same thing as the first, and much more.
Just one more tool for the toolbox!
Useful Resources
If you don’t know Allen Browne’s website, I highly urge to review it as it has some serious gems there!
