文字符號_括弧、頓號_分離器(X(X1、X2)=> 'X,X1,X2)

'例如:「多麗(綠頭鴨、鴨頭綠)」=>此一字串可以分出「多麗」,「綠頭鴨」,「鴨頭綠」三個字串) '又如:「x(x1、x2、x3、……)」可析離出「x」「x1」「x2」「x3」……
這是我昨晚寫的。以後碰到像這樣有個正文、後加括弧,內又或有頓號隔開的,便可以直接套用,輕鬆處理了,好像文字處理函式的擴充方法(如前天 C# 所學的 extension methods ):
Extension methods enable you to "add" methods to existing types without creating a new derived type,…… http://bit.ly/2tZOZ8L
Private Sub List2_AfterUpdate() 'List2=詞牌名清單
Dim x As String, xList2() As String, i As Integer'x : 原字串、xList2: 新字串
x = List2.Value
List2.Enabled = False

If x Like "*[(、]*" Then '多麗(綠頭鴨、鴨頭綠)
    If InStr(x, "、") > 0 Then ' 有頓號
        i = 1
        ReDim Preserve xList2(i)
        xList2(0) = Mid(x, 1, InStr(x, "(") - 1)
        xList2(1) = Mid(x, InStr(x, "(") + 1, InStr(x, "、") - InStr(x, "(") - 1)
        Do Until InStr(x, "、") = 0
            i = i + 1
            ReDim Preserve xList2(i)
            x = Mid(x, InStr(x, "、") + 1)
            If InStr(x, "、") = 0 Then
                x = Replace(x, ")", "")
                xList2(i) = x
            Else
                xList2(i) = Mid(x, 1, InStr(x, "、") - 1)
            End If
        Loop
        
    Else
        ReDim xList2(1)
        If InStr(x, ")") > 0 Then '只有()
            x = Mid(x, InStr(x, "(") + 1, Len(x) - InStr(x, "(") - 1)
            xList2(1) = x
            xList2(0) = Replace(List2.Value, "(" & x & ")", "")
        Else
            xList2(0) = x
            x = Mid(x, InStr(x, "(") + 1)
            xList2(1) = x
            xList2(0) = Replace(List2.Value, "(" & x, "")
        End If
    End If
    qy1FieldValuesOr List2.Tag, xList2, False
Else
    testADO List2.Tag, x
End If

List2.Enabled = True
End Sub

留言

熱門文章