在Windows 7上使用MS Access 2003,我发现以下函数会从ANSI字符串中删除所有重音,更改(例如)señor到senor:
Public Function RemoveAccents(ByVal inputString As String) As String
Const accentString As String = "ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÏÑÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðñòóóôõöùúûüýÿ"
Const nonAccentStr As String = "AAAAAACEEEEIIIIINOOOOOUUUUYaaaaaaceeeeiiiionoooooouuuuyy"
Dim i As Integer
For i = 1 To Len(accentString)
inputString = Replace(inputString, Mid(accentString, i, 1), Mid(nonAccentStr, i, 1), , , vbBinaryCompare)
Next i
RemoveAccents = inputString
End Function
但是当我尝试将带有Caron(U-010D)(č)(HTMLč)的拉丁小写字母C添加到函数Const accentString时,就像这样,
Const accentString As String = "ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÏÑÒÓÔÕÖÙÚÛÜÝàáâãäåç" & ChrW$(&H10D) & "èéêëìíîïðñòóóôõöùúûüýÿ"
Const nonAccentStr As String = "AAAAAACEEEEIIIIINOOOOOUUUUYaaaaaacceeeeiiiionoooooouuuuyy"
我无法运行该功能。是否有一种语法允许我调整此函数以从包含不在ANSI字符集中的字符的字符串中去除变音符号?
答案 0 :(得分:4)
您不能在常量声明中使用ChrW()
之类的函数。
<强>修强>
如果您可以使用这些公共变量而不是常量,那么这应该为您处理:
Const cWithCaron As String = &H10D
Public accentString As String
Public nonAccentStr As String
Sub TestStrings()
Dim clnString As String
accentString = "ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÏÑÒÓÔÕÖÙÚÛÜÝàáâãäåç" & ChrW(cWithCaron) & "èéêëìíîïðñòóóôõöùúûüýÿ"
nonAccentStr = "AAAAAACEEEEIIIIINOOOOOUUUUYaaaaaacceeeeiiiionoooooouuuuyy"
'I added this variable to test the function:
clnString = RemoveAccents(accentString)
'And a message box to display the results:
MsgBox clnString = nonAccentStr
End Sub
Public Function RemoveAccents(ByVal inputString As String) As String
Dim i As Integer
For i = 1 To Len(accentString)
inputString = Replace(inputString, Mid(accentString, i, 1), Mid(nonAccentStr, i, 1), , , vbBinaryCompare)
Next i
RemoveAccents = inputString
End Function