如何在不损失速度或可读性的情况下重写这个来删除这些?

bug*_*net 0 string vb6 split goto

我写了这个.是的,我知道它是VB6.是的,它是生产代码,是的,我知道它使用了gotos.我是一个懒惰的,邪恶的野兽......

所以告诉我(以及我们其他人)应该如何写

Public Function SplitString(ByVal sText As Variant) As Variant
    Dim nHere As Long
    Dim cHere As String * 1
    Dim aRes As Variant
    Dim nRes As Long
    Dim bInquote As Boolean
    Dim sString As String
    ReDim aRes(0)
    nHere = 1
    nRes = 0
    Do
    If nHere > Len(sText) Then Exit Do
    cHere = Mid$(sText, nHere, 1)
    If cHere = Chr$(32) Then
        If bInquote Then
        sString = sString & cHere
        GoTo nextChar
        End If
        If sString <> vbNullString Then
        aRes(nRes) = sString
        sString = vbNullString
        nRes = nRes + 1
        ReDim Preserve aRes(nRes)
        End If
        GoTo nextChar
    ElseIf cHere = Chr$(34) Then
        bInquote = Not bInquote
        GoTo nextChar
    Else
        sString = sString & cHere
    End If
nextChar:
    nHere = nHere + 1
    Loop
    If sString <> vbNullString Then
    aRes(nRes) = sString
    End If
    SplitString = aRes
End Function
Run Code Online (Sandbox Code Playgroud)

顺便说一下,它将一个字符串拆分成一个数组.可以引用字符串中的元素.

TcK*_*cKs 6

这很简单:

将"如果sString <> vbNullString然后"改为"ElseIf sString <> vbNullString Then",删除所有"转到"并删除"nextChar:".

Public Function SplitString(ByVal sText As Variant) As Variant
    Dim nHere As Long
    Dim cHere As String * 1
    Dim aRes As Variant
    Dim nRes As Long
    Dim bInquote As Boolean
    Dim sString As String
    ReDim aRes(0)
    nHere = 1
    nRes = 0
    Do
        If nHere > Len(sText) Then Exit Do
        cHere = Mid$(sText, nHere, 1)
        If cHere = Chr$(32) Then
            If bInquote Then
                sString = sString & cHere
            ElseIf sString <> vbNullString Then
                aRes(nRes) = sString
                sString = vbNullString
                nRes = nRes + 1
                ReDim Preserve aRes(nRes)
            End If
        ElseIf cHere = Chr$(34) Then
            bInquote = Not bInquote
        Else
            sString = sString & cHere
        End If

        nHere = nHere + 1
    Loop
    If sString <> vbNullString Then
        aRes(nRes) = sString
    End If
    SplitString = aRes
End Function
Run Code Online (Sandbox Code Playgroud)