更改要删除的代码,而不是允许字符 [英] Alter code to Remove instead of Allow characters

查看:32
本文介绍了更改要删除的代码,而不是允许字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

函数允许列出的字符(在这种情况下)

The function allows the character listed (in this case)

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

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

Is it possible to change this to Remove the characters listed instead?

谢谢

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

推荐答案

如果您打算使用 For ...下一步-只需添加 Case Else :

If you intend to use For ... Next - just add 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效率更高,在您的情况下,功能可能如下:

As @pnuts pointed and @brettdj answered - RegEx is more efficient, in your case the function may be as follows:

Function NonAlphaNumericOnly(strSource As String) As String
    With CreateObject("VBScript.RegExp")
        .Global = True
        .IgnoreCase = True
        .Pattern = "[\w]+"
        NonAlphaNumericOnly = .Replace(strSource, "")
    End With
End Function

这篇关于更改要删除的代码,而不是允许字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆