0

我有一个像X5BC8373XXX. 其中 X = 特殊字符等于 Square。
我也有一些特殊字符,\n但我删除了它们,但我无法删除方块......我想知道如何删除它。
我找到了这个方法:

Dim Test As String
Test = Replace(Mscomm1.Input, Chr(160), Chr(64) 'Here I remove some of the special characters like \n
Test = Left$(Test, Len(Test) -2)
Test = Right$(Test, Len(Test) -2)

此方法确实删除了那些特殊字符,但它也删除了我的第一个字符5
我意识到这种方法只是从left和 中删除 2 个字符right
但是我该如何解决这个问题以删除这些特殊字符?

我也看到了vblF, CtrlF类似这样的东西,但我无法使用它;\

4

3 回答 3

4

您可以使用正则表达式。如果您想删除不是数字或字母的所有内容,可以使用下面的代码。如果您想保留其他字符,则正则表达式是高度可定制的,但可能会有些混乱。

这还具有一次执行整个字符串的好处,而不是一个字符一个字符。

您需要在项目中引用Microsoft VBScript 正则表达式

Function AlphaNum(OldString As String)
   Dim RE As New RegExp
   RE.Pattern = "[^A-Za-z0-9]"
   RE.Global = True
   AlphaNum = RE.Replace(OldString, "")
End Function
于 2013-06-29T02:11:04.877 回答
3

清除不可打印的字符很容易。一种蛮力但易于定制的方法可能是:

Private Function Printable(ByVal Text As String) As String
    Dim I As Long
    Dim Char As String
    Dim Count As Long

    Printable = Text 'Allocate space, same width as original.
    For I = 1 To Len(Text)
        Char = Mid$(Text, I, 1)
        If Char Like "[ -~]" Then
            'Char was in the range " " through "~" so keep it.
            Count = Count + 1
            Mid$(Printable, Count, 1) = Char
        End If
    Next
    Printable = Left$(Printable, Count)
End Function

Private Sub Test()
    Dim S As String

    S = vbVerticalTab & "ABC" & vbFormFeed & vbBack
    Text1.Text = S 'Shows "boxes" or "?" depending on the font.
    Text2.Text = Printable(S)
End Sub
于 2013-06-28T21:50:03.510 回答
0

这将删除控制字符(低于 CHR(32))

Function CleanString(strBefore As String) As String
  CleanString = ""
  Dim strAfter As String
  Dim intAscii As Integer
  Dim strTest As String
  Dim dblX As Double
  Dim dblLen As Double
  intLen = Len(strBefore)
  For dblX = 1 To dblLen
    strTest = Mid(strBefore, dblX, 1)
    If Asc(strTest) < 32 Then
      strTest = " "
    End If
    strAfter = strAfter & strTest
  Next dblX
  CleanString = strAfter
End Function
于 2021-04-20T17:26:04.597 回答