删除字符串中的Unicode字符

问题描述 投票:4回答:6

如何删除VBA中不属于ASCII类别的所有特殊字符?

这些是出现在我的字符串中的一些符号。

Œ–ŠšŸƒ

还有更多这样的字符。

这些不属于ASCII类别,因为您可以在这里看到http://www.ascii.cl/htmlcodes.htm

我尝试过类似的事情

strName = Replace(strName, ChrW(376), " ")
excel vba unicode non-ascii-characters
6个回答
6
投票

您是否会感兴趣RegEx解决方案?

此站点上有很多针对不同语言的示例-这里是C#:How can you strip non-ASCII characters from a string? (in C#)

尝试使用VBA:

Private Function GetStrippedText(txt As String) As String
    Dim regEx As Object

    Set regEx = CreateObject("vbscript.regexp")
    regEx.Pattern = "[^\u0000-\u007F]"
    GetStrippedText = regEx.Replace(txt, "")

End Function

1
投票

在立即窗口中编写以下内容会得到什么?

?Replace("ŸŸŸŸ", ChrW(376), "ale")

我得到:alealealeale


1
投票

尝试以下内容

Function ClearUnwantedString(fulltext As String) As String
    Dim output As String
    Dim character As String
    For i = 1 To Len(fulltext)
        character = Mid(fulltext, i, 1)
        If (character >= "a" And character <= "z") Or (character >= "0" And character <= "9") Or (character >= "A" And character <= "Z") Then
            output = output & character
        End If
    Next
    ClearUnwantedString = output
End Function

Sub test()
    a = ClearUnwantedString("dfjŒœŠdskl")
End Sub

0
投票

尝试application.clean()

它将删除所有不可打印的字符


0
投票

假设您有:

enter image description here

然后下面的代码将从String中获得A1并仅允许ANSI中的A2(代码0至255)通过。

Sub test()
 Dim s1 As String, s2 As String, c As String, i As Long, iAsc As Integer

 s1 = Range("A1").Value

 s2 = ""

 For i = 1 To Len(s1)
  c = Mid(s1, i, 1)
  iAsc = AscW(c)
  If iAsc <= 255 Then
   s2 = s2 & c
  End If
 Next

 Range("A2").Value = s2

End Sub

0
投票

不需要循环每个字符

也许晚了,但也许对某人有帮助:

Public Function StripNonAsciiChars(ByVal InputString As String) As String
    Dim i As Integer
    Dim RegEx As Object
    Set RegEx = CreateObject("VBScript.RegExp")
    With RegEx
        .Global = True
        .MultiLine = True
        .IgnoreCase = True
        .Pattern = "[^\u0000-\u007F]"
        StripNonAsciiChars = Application.WorksheetFunction.Trim(RegEx.Replace(InputString, " "))
    End With
End Function
© www.soinside.com 2019 - 2024. All rights reserved.