(整)字符串提取相关的四个函数

字符串提取相关的四个函数

Function GetNumVal(Str As String, v_I As Integer) As Double
‘ 功能说明:
‘ 在Str中指定的位置上(这里是[])截取字符

GetNumVal = 0

Dim i As Integer
Dim cnt As Integer
    cnt = 0
    If Str = "" Then Exit Function
    For i = 1 To Len(Str)
        i = InStr(i, Str, "[")
        If i = 0 Then Exit Function
        If Mid(Str, i, 1) = "[" Then
            cnt = cnt + 1
            If cnt = v_I Then
                If InStr(i + 1, Str, "]") = 0 Then
                    Exit Function
                Else
                    If IsNumeric(Mid(Str, i + 1, InStr(i + 1, Str, "]") - i - 1)) Then
                        GetNumVal = (Mid(Str, i + 1, InStr(i + 1, Str, "]") - i - 1))
                    End If
                End If
                Exit For
            End If
        End If
    Next i
   
End Function

Function GetStrNumStr(Str As String, v_I As Integer) As String
‘ 功能说明:
‘ 在Str中指定的位置上指定截取字符的位置

GetStrNumStr = " "

Dim i As Integer
Dim cnt As Integer
    cnt = 0
    If Str = "" Then Exit Function
    For i = 1 To Len(Str)
        i = InStr(i, Str, "[")
        If i = 0 Then Exit Function
        If Mid(Str, i, 1) = "[" Then
            cnt = cnt + 1
            If cnt = v_I Then
                If InStr(i + 1, Str, "]") = 0 Then
                    Exit Function
                Else
                    If IsNumeric(Mid(Str, i + 1, InStr(i + 1, Str, "]") - i - 1)) Then
                        GetStrNumStr = Mid(Str, InStr(i + 1, Str, "]") + 1, 1)
                    End If
                End If
                Exit For
            End If
        End If
    Next i
   
    If GetStrNumStr = "|" Then
        GetStrNumStr = " or "
    ElseIf GetStrNumStr = "&" Then
        GetStrNumStr = " and "
    Else
        GetStrNumStr = " "
    End If
   
End Function

Function GetNumStr(Str As String, v_I As Integer, Optional strDefault As String) As String
‘ 功能说明:
‘ 在Str中指定的位置上(这里是[])截取字符

GetNumStr = ""
If Str = "" Then ‘如果传入的值是空,则函数的值就等于默认值
    GetNumStr = strDefault
End If

Dim i As Integer
Dim cnt As Integer
    cnt = 0
    If Str = "" Then Exit Function
    For i = 1 To Len(Str)
        i = InStr(i, Str, "[")
        If i = 0 Then Exit Function
        If Mid(Str, i, 1) = "[" Then
            cnt = cnt + 1
            If cnt = v_I Then
                If InStr(i + 1, Str, "]") = 0 Then
                    Exit Function
                Else
                    ‘If Mid(Str, i + 1, InStr(i + 1, Str, "]") - i - 1)) Then
                        GetNumStr = (Mid(Str, i + 1, InStr(i + 1, Str, "]") - i - 1))
                    ‘End If
                End If
                Exit For
            End If
        End If
    Next i
   
    If GetNumStr = "" And strDefault <> "" Then
        GetNumStr = strDefault
    End If
   
   
End Function

Function GetNumStr1(Str As String, v_I As Integer, Optional strDefault As String) As String
‘ 功能说明:
‘ 在Str中指定的位置上(这里是{})截取字符

GetNumStr1 = ""
If Str = "" Then ‘如果传入的值是空,则函数的值就等于默认值
    GetNumStr1 = strDefault
End If

Dim i As Integer
Dim cnt As Integer
    cnt = 0
    If Str = "" Then Exit Function
    For i = 1 To Len(Str)
        i = InStr(i, Str, "{")
        If i = 0 Then Exit Function
        If Mid(Str, i, 1) = "{" Then
            cnt = cnt + 1
            If cnt = v_I Then
                If InStr(i + 1, Str, "}") = 0 Then
                    Exit Function
                Else
                    ‘If Mid(Str, i + 1, InStr(i + 1, Str, "}") - i - 1)) Then
                        GetNumStr1 = (Mid(Str, i + 1, InStr(i + 1, Str, "}") - i - 1))
                    ‘End If
                End If
                Exit For
            End If
        End If
    Next i
   
    If GetNumStr1 = "" And strDefault <> "" Then
        GetNumStr1 = strDefault
    End If
   
   
End Function

Random Posts

Leave a Reply