I'm unsure if something like this already exists somewhere or not. Maybe someone has already programmed a better function than this. I do occasionally need to get a certain portion of a text string like the last part of an email address for example. Here's a fairly powerful function that will return all characters in a string up to a specified "stop value". You can opt to included the stop value if you like. You can also specify that you want all characters up to the nth incident of the specified stop value.
I'm posting here in hopes that A) someone can look it over and point out any flaws they see in the way the function is designed or even in the naming of it B) that the function will be of value to someone else.
Usage:
I'm posting here in hopes that A) someone can look it over and point out any flaws they see in the way the function is designed or even in the naming of it B) that the function will be of value to someone else.
Code:
Public Function fGetCharsUpTo(sString As String, sStopChar As String, _
Optional bIncludeStopChar As Boolean = False, _
Optional iIncident As Integer = 1, _
Optional bLeftToRight As Boolean = True) As String
'This function is designed to return everything in a string up to the specified character
'It also allows you to return the specified stop character as well as
'specify which incidence of the given stop character you'd like to stop at
'Examples:
'fGetCharsUpTo("someone@email.com", "@", False, 1, True) 'returns 'someone'
'fGetCharsUpTo("someone@email.com", "@", False, 1, False) 'returns 'email.com'
'fGetCharsUpTo("someone@email.com", "@", True, 1, True) 'returns 'someone@'
'fGetCharsUpTo("someone@email.com", "@", True, 1, False) 'returns '@email.com'
'fGetCharsUpTo("3.75", ".", True, 1, False) 'returns '.75'
'fGetCharsUpTo("3.75", ".", False, 1, False) 'returns '75'
'fGetCharsUpTo("3.75", ".", True, 1, True) 'returns '3.'
'fGetCharsUpTo("3.75", ".", False, 1, True) 'returns '3'
'fGetCharsUpTo("C:\Documents and Settings\John Davis\My Documents", "\", False, 1, False) 'returns 'My Documents'
'fGetCharsUpTo("C:\Documents and Settings\John Davis\My Documents", "\", True, 1, False) 'returns '\My Documents'
'fGetCharsUpTo("C:\Documents and Settings\John Davis\My Documents", "\", False, 2, True) 'returns 'C:\Documents and Settings'
'fGetCharsUpTo("C:\Documents and Settings\John Davis\My Documents", "\", True, 2, True) 'returns 'C:\Documents and Settings\'
'fGetCharsUpTo("C:\Documents and Settings\John Davis\My Documents", "\", False, 3, True) 'returns 'C:\Documents and Settings\John Davis'
'fGetCharsUpTo("C:\Documents and Settings\John Davis\My Documents", "\", True, 3, True) 'returns 'C:\Documents and Settings\John Davis\'
fGetCharsUpTo = "Error"
If sString = "" Or sStopChar = "" Or iIncident < 1 Then
'We need to at least have a string and a stop character passed in
'0 or less is not a legitimate value for iIncident
Exit Function
Else
'Let's first check to see if the specified stop character actually exists
If InStr(sString, sStopChar) = 0 Then
'Stop character doesn't even exist in the specified string
fGetCharsUpTo = sString
Exit Function
End If
Dim s As String, iLen As Integer, iIncdnt As Integer, i As Long
iIncdnt = 1
If bLeftToRight = True Then
iLen = Len(sString)
Do Until i = iLen
If Left(sString, 1) <> sStopChar Then
s = s & Left(sString, 1)
sString = right(sString, Len(sString) - 1) 'take one off
i = i + 1
Else
If iIncdnt = iIncident Then
i = iLen
If bIncludeStopChar = True Then s = s & Left(sString, 1)
Else
s = s & Left(sString, 1)
sString = right(sString, Len(sString) - 1) 'take one off
i = i + 1
iIncdnt = iIncdnt + 1
End If
End If
Loop
fGetCharsUpTo = s
Else
iLen = Len(sString)
Do Until iLen = 0
If right(sString, 1) <> sStopChar Then
s = right(sString, 1) & s
sString = Left(sString, Len(sString) - 1) 'take one off
iLen = Len(sString)
Else
If iIncdnt = iIncident Then
iLen = 0
If bIncludeStopChar = True Then s = right(sString, 1) & s
Else
iIncdnt = iIncdnt + 1
End If
End If
Loop
fGetCharsUpTo = s
End If
End If
End Function
Usage:
Code:
MsgBox fGetCharsUpTo("someone@email.com", "@", False, 1, True) 'returns 'someone'
MsgBox fGetCharsUpTo("someone@email.com", "@", False, 1, False) 'returns 'email.com'
MsgBox fGetCharsUpTo("someone@email.com", "@", True, 1, True) 'returns '@someone'
MsgBox fGetCharsUpTo("someone@email.com", "@", True, 1, False) 'returns '@email.com'
MsgBox fGetCharsUpTo("3.75", ".", True, 1, False) 'returns '.75'
MsgBox fGetCharsUpTo("3.75", ".", False, 1, False) 'returns '75'
MsgBox fGetCharsUpTo("3.75", ".", True, 1, True) 'returns '3.'
MsgBox fGetCharsUpTo("3.75", ".", False, 1, True) 'returns '3'
MsgBox fGetCharsUpTo("C:\Documents and Settings\John Davis\My Documents", "\", False, 1, False) 'returns 'My Documents'
MsgBox fGetCharsUpTo("C:\Documents and Settings\John Davis\My Documents", "\", True, 1, False) 'returns '\My Documents'
MsgBox fGetCharsUpTo("C:\Documents and Settings\John Davis\My Documents", "\", False, 2, True) 'returns 'C:\Documents and Settings'
MsgBox fGetCharsUpTo("C:\Documents and Settings\John Davis\My Documents", "\", True, 2, True) 'returns 'C:\Documents and Settings\'
MsgBox fGetCharsUpTo("C:\Documents and Settings\John Davis\My Documents", "\", False, 3, True) 'returns 'C:\Documents and Settings\John Davis'
MsgBox fGetCharsUpTo("C:\Documents and Settings\John Davis\My Documents", "\", True, 3, True) 'returns 'C:\Documents and Settings\John Davis\'