Visual Basic Programming Code Examples
Visual Basic > Strings Code Examples
Get strings on either side of special character.
Get strings on either side of special character.
'Here is a function that will get the string on each side of
'a string that is really two strings, with a special char in
'the center.
Function GetString(tmpStr As String, tmpDiv As String, Mode As String) As
String
Dim tmpRetStr As String
Dim tmpLen As Integer
Dim tmpErr As Integer
'
' tmpString = The whole string
' tmpDiv = The Divider chr
' if mode = "F" then get the string in front of the div
' if mode = "B" then get the string in back of the div
'
' Return values:
' Errors
' Err1 = wrong mode ("F" & "B" ok)
' Err2 = No Div, did not found a divider
' Err3 = No F, did not find any string in front of the div
' Err4 = No B, did not find any string in back of the div
' Err5 = No String
' Normal
' String
'
' Example: GetString("Red=Green","=","F") will retrun "Red"
' *** for string
tmpErr = 0
If Len(tmpStr) = 0 Then
tmpErr = 5
GoTo GetStringErr
End If
' *** test for chr in tmpDiv
If Len(tmpDiv) = 0 Then
tmpErr = 2
GoTo GetStringErr
End If
' *** test for div in string
If InStr(1, tmpStr, tmpDiv) = 0 Then
tmpErr = 2
GoTo GetStringErr
End If
' *** process "F"
If Mode = "F" Then
tmpRetStr = Left(tmpStr, (InStr(1, tmpStr, tmpDiv) - 1))
If Len(tmpRetStr) > 0 Then
GetString = tmpRetStr
Exit Function
Else
tmpErr = 3
GoTo GetStringErr
End If
End If
If Mode = "B" Then
tmpRetStr = Right(tmpStr, Len(tmpStr) - (InStr(1, tmpStr, tmpDiv)))
If Len(tmpRetStr) > 0 Then
GetString = tmpRetStr
Exit Function
Else
tmpErr = 4
GoTo GetStringErr
End If
End If
tmpErr = 1
GoTo GetStringErr
Exit Function
GetStringErr:
GetString = "Err" & tmpErr
End Function