Reputation: 179
I need to convert the following vb.net code to vba for my ms access database, to be able to convert the Arabic numerical string "٠١٢٣٤٥٦٧٨٩"
to an English numerical string "0123456789"
, i found the code in this url 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
Upvotes: 0
Views: 2421
Reputation: 32682
You can use the following function:
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
This executes a replace for every arabic number, and replaces it to the latin equivalent.
Note that you need to adjust the locale settings to accept arabic symbols in the VBA editor (see this question)
Alternatively, if you don't want to adjust your locale settings:
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
Note that this doesn't include the dot, but as specified in the question, only numbers need replacement.
Upvotes: 3
Reputation: 5986
You can build your own Scripting.Dictionary
for that task:
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
Upvotes: 1
Reputation: 8033
You need to set the cultureInfo for the appendFormat method to 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
Upvotes: 1