删除字符串中的 Unicode 字符
Remove Unicode characters in a String
如何删除所有不属于 VBA 中 ASCII 类别的特殊字符?
这些是出现在我的字符串中的一些符号。
Œ œ Š š Ÿ ƒ
这样的人物还有很多。
正如您在此处看到的那样,这些不属于 ASCII 类别http://www.ascii.cl/htmlcodes.htm
我试过这样的东西
strName = Replace(strName, ChrW(376), " ")
尝试application.clean()
它将删除所有不可打印的字符
立即写下以下内容会得到什么window?
?Replace("ŸŸŸŸ", ChrW(376), "ale")
我得到:
alalealeale
假设你有:
然后下面的代码会从A1
中得到String
并且只让A2
中的ANSI
(代码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
尝试以下
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
您会对 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
不需要循环每个字符
可能晚了,但也许对某人有帮助:
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
如何删除所有不属于 VBA 中 ASCII 类别的特殊字符?
这些是出现在我的字符串中的一些符号。
Œ œ Š š Ÿ ƒ
这样的人物还有很多。
正如您在此处看到的那样,这些不属于 ASCII 类别http://www.ascii.cl/htmlcodes.htm
我试过这样的东西
strName = Replace(strName, ChrW(376), " ")
尝试application.clean()
它将删除所有不可打印的字符
立即写下以下内容会得到什么window?
?Replace("ŸŸŸŸ", ChrW(376), "ale")
我得到: alalealeale
假设你有:
然后下面的代码会从A1
中得到String
并且只让A2
中的ANSI
(代码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
尝试以下
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
您会对 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
不需要循环每个字符
可能晚了,但也许对某人有帮助:
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