将任何阿拉伯数字转换为英文数字

时间:2017-11-30 15:24:23

标签: vb.net vba access-vba

我需要将以下vb.net代码转换为我的ms访问数据库的vba,以便能够将阿拉伯数字字符串"٠١٢٣٤٥٦٧٨٩"转换为英文数字字符串"0123456789",我找到了代码在此网址convert Arabic numerical to English

Private Shared Function ArabicToWestern(ByVal input As String) As String
    Dim western As StringBuilder = New StringBuilder

    For Each num As Char In input
        western.Append(Char.GetNumericValue(num))
    Next

    Return western.ToString
End Function

3 个答案:

答案 0 :(得分:2)

您可以使用以下功能:

Public Function ReplaceArabicNumbers(strInput As String) As String
    Dim numberArray: numberArray = Array("٠", "0", "١", "1", "٢", "2", "٣", "3", "٤", "4", "٥", "5", "٦", "6", "٧", "7", "٨", "8", "٩", "9")
    Dim i As Long
    ReplaceArabicNumbers = strInput
    For i = 0 To 18 Step 2
        ReplaceArabicNumbers = Replace(ReplaceArabicNumbers, numberArray(i), numberArray(i + 1))
    Next i
End Function

这会对每个阿拉伯数字执行替换,并将其替换为拉丁语等号。

请注意,您需要调整区域设置以接受VBA编辑器中的阿拉伯语符号(请参阅this question

或者,如果您不想调整区域设置:

Public Function ReplaceArabicNumbers(strInput As String) As String
    Dim numberArray: numberArray = Array(ChrW(&H660), "0", ChrW(&H661), "1", ChrW(&H662), "2", ChrW(&H663), "3", ChrW(&H664), "4", ChrW(&H665), "5", ChrW(&H666), "6", ChrW(&H667), "7", ChrW(&H668), "8", ChrW(&H669), "9")
    Dim i As Long
    ReplaceArabicNumbers = strInput
    For i = 0 To 18 Step 2
        ReplaceArabicNumbers = Replace(ReplaceArabicNumbers, numberArray(i), numberArray(i + 1))
    Next i
End Function

请注意,这并不包含点,但正如问题中所指定的,只需要替换数字。

答案 1 :(得分:2)

您可以为该任务构建自己的Scripting.Dictionary

Public Function ReplaceArabicNumbers(intpt As String) As String

' Select Tools->References from the Visual Basic menu.
' Check box beside "Microsoft Scripting Runtime" in the list.

    dict.Add Key:=ChrW(&H661), Item:=0
    dict.Add Key:=ChrW(&H662), Item:=1
    dict.Add Key:=ChrW(&H663), Item:=2
    dict.Add Key:=ChrW(&H664), Item:=3

   ' OR ALTERNATIVALY
   ' dict.Add Key:="٠", Item:=0
   ' dict.Add Key:="١", Item:=1
   ' dict.Add Key:="٢", Item:=2
   ' dict.Add Key:="٣", Item:=3

    Dim s As String
    Dim Counter As Integer


    For Counter = 1 To Len(intpt)
        If dict.Exists(Mid(intpt, Counter, 1)) Then
        s = s & dict(Mid(intpt, Counter, 1))
        Else
        s = s & Mid(intpt, Counter, 1)
        End If


    Next Counter

    ReplaceArabicNumbers = s

End Function

答案 2 :(得分:1)

您需要将appendFormat方法的cultureInfo设置为US

Private Shared Function ArabicToWestern(ByVal input As String) As String
    Dim western As StringBuilder = New StringBuilder
    Dim ci As CultureInfo = New CultureInfo("en-US", True)
    For Each num As Char In input
        western.AppendFormat(ci, "{0}", Char.GetNumericValue(num))
    Next

    Return western.ToString
End Function