1

列出的字符的功能allows(在这种情况下)

Case 48 To 57, 65 To 90, 97 To 122:

是否可以将其更改为Remove列出的字符?

谢谢

Function AlphaNumericOnly(strSource As String) As String
    Dim i As Integer
    Dim strResult As String

    For i = 1 To Len(strSource)
        Select Case Asc(Mid(strSource, i, 1))
            Case 48 To 57, 65 To 90, 97 To 122:
                strResult = strResult & Mid(strSource, i, 1)
        End Select
    Next
    AlphaNumericOnly = strResult
End Function
4

1 回答 1

1

如果您打算使用For ... Next- 只需添加Case Else

    For i = 1 To Len(strSource)
        Select Case Asc(Mid(strSource, i, 1))
            Case 48 To 57, 65 To 90, 97 To 122:
            Case Else:
                strResult = strResult & Mid(strSource, i, 1)
        End Select
    Next

正如@pnuts 指出和@brettdj回答的那样- RegEx 更有效,在您的情况下,函数可能如下:

Function NonAlphaNumericOnly(strSource As String) As String
    With CreateObject("VBScript.RegExp")
        .Global = True
        .IgnoreCase = True
        .Pattern = "[\w]+"
        NonAlphaNumericOnly = .Replace(strSource, "")
    End With
End Function
于 2015-08-24T22:52:06.800 回答