MS Dynamics有一个问题,它不允许x20到x7f以外的任何字符,并且该范围内的某些字符也是无效的。我的答案是创建一个键入无效字符的数组,以返回有效字符的最佳猜测。
它不漂亮,但它的工作原理。
Function PlainAscii(InText)
Dim i, c, a
Const cUTF7 = "^[\x20-\x7e]+$"
Const IgnoreCase = False
PlainAscii = ""
If InText = "" Then Exit Function
If RegExTest(InText, cUTF7, IgnoreCase) Then
PlainAscii = InText
Else
For i = 1 To Len(InText)
c = Mid(InText, i, 1)
a = Asc(c)
If a = 10 Or a = 13 Or a = 9 Then
' Do Nothing - Allow LF, CR & TAB
ElseIf a < 32 Then
c = " "
ElseIf a > 126 Then
c = CvtToAscii(a)
End If
PlainAscii = PlainAscii & c
Next
End If
End Function
Function CvtToAscii(inChar)
' Maps The Characters With The 8th Bit Set To 7 Bit Characters
Dim arrChars
arrChars = Array(" ", " ", "$", " ", ",", "f", """", " ", "t", "t", "^", "%", "S", "<", "O", " ", "Z", " ", " ", "'", "'", """", """", ".", "-", "-", "~", "T", "S", ">", "o", " ", "Z", "Y", " ", "!", "$", "$", "o", "$", "|", "S", " ", "c", " ", " ", " ", "_", "R", "_", ".", " ", " ", " ", " ", "u", "P", ".", ",", "i", " ", " ", " ", " ", " ", " ", "A", "A", "A", "A", "A", "A", "A", "C", "E", "E", "E", "E", "I", "I", "I", "I", "D", "N", "O", "O", "O", "O", "O", "X", "O", "U", "U", "U", "U", "Y", "b", "B", "a", "a", "a", "a", "a", "a", "a", "c", "e", "e", "e", "e", "i", "i", "i", "i", "o", "n", "o", "o", "o", "o", "o", "/", "O", "u", "u", "u", "u", "y", "p", "y")
CvtToAscii = arrChars(inChar - 127)
End Function
Function RegExTest(ByVal strStringToSearch, strExpression, IgnoreCase)
Dim objRegEx
On Error Resume Next
Err.Clear
strStringToSearch = Replace(Replace(strStringToSearch, vbCr, ""), vbLf, "")
RegExTest = False
Set objRegEx = New RegExp
With objRegEx
.Pattern = strExpression '//the reg expression that should be searched for
If Err.Number = 0 Then
.IgnoreCase = CBool(IgnoreCase) '//not case sensitive
.Global = True '//match all instances of pattern
RegExTest = .Test(strStringToSearch)
End If
End With
Set objRegEx = Nothing
On Error Goto 0
End Function
您的答案肯定会有所不同。
你是如何定义“最接近的ASCII等价物”?只需删除重音标记? – mmr 2010-04-12 19:12:51
那么......如果有任何定义,除了转换为?我想它:)我认为在口音的情况下,它只是删除。 – Andrey 2010-04-12 19:15:20
@mmr:我不知道安德烈的用法,但我有一个应该接受文本的程序,并将它提供给一个连接的设备,稍后将显示它。多种连接的设备无法承担存储多字节字符所需的RAM,并且无论如何,无法承受存储超过256个字符字形所需的ROM [大多数字符矩阵LCD模块具有固定的160个字符设置加显示八个同时自定义5x7或5x8字形的能力]。将所有非ASCII文本呈现为“?”似乎是不必要的丑陋。 – supercat 2014-01-15 19:24:25