问题
i am looking for a way to convert the Arabic numerical string "٠١٢٣٤٥٦٧٨٩" to an English numerical string "0123456789"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
dim Anum as string ="٠١٢٣٤٥٦٧٨٩"
dim Enum as string =get_egnlishNum(Anum)
End Sub
private function get_egnlishNum(byval _Anum as string) as string
'' converting code
end function
回答1:
You're looking for the GetNumericValue
method of the char
type which converts any numeric Unicode character to a double. For example:
double two = char.GetNumericValue('٢');
Console.WriteLine(two); // prints 2
For your example:
static string ArabicToWestern(string input)
{
StringBuilder western = new StringBuilder();
foreach(char num in input)
{
western.Append(char.GetNumericValue(num));
}
return western.ToString();
}
Modify per your needs.
VB.NET:
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
回答2:
This is one of the solutions.
Function Convert(ByVal input As String) As String
Dim source = "٠١٢٣٤٥٦٧٨٩"
Dim target = "0123456789"
Dim sb As New StringBuilder()
For Each el in Input
sb.Append(target(source.IndexOf(el)))
Next
Return sb.ToString
End Function
EDIT
I've tried to find out more "native" ways. What I found is the NativeDigits property of NumberFormatInfo class
This was my test code but it didn't succeed. But it can be a good starting point.
Dim source = "١٢٣٤٥٦٧٨٩"
Dim result As Integer
Dim numInfo As new NumberFormatInfo()
numInfo.NativeDigits = New String() { "٠", "١", "٢", "٣", "٤", "٥", "٦", "٧", "٨", "٩" }
Int32.TryParse(source, NumberStyles.Any, numInfo, result)
回答3:
You can simply replace the Arabic characters with the Western versions:
Dim arabicDigits = "٠١٢٣٤٥٦٧٨٩".ToCharArray
Dim s = "٠٦٦٢٧٣٩٦عدد النقاط هي ٩٩٣٩٣٥"
For i = 0 To arabicDigits.Length - 1
s = s.Replace(arabicDigits(i), i.ToString)
Next
s now contains "06627396عدد النقاط هي 993935"
回答4:
Dim arabicDigits = "٠١٢٣٤٥٦٧٨٩".ToCharArray
dim i as integer=0
For i = 0 To anum.Length - 1
select case arabicDigits(i)
case "٠"
Replace(arabicDigits(i),"0")
case "١"
Replace(arabicDigits(i),"1")
case "٢"
Replace(arabicDigits(i),"2")
case "٣"
Replace(arabicDigits(i),"3")
case "٤"
Replace(arabicDigits(i),"4")
case "٥"
Replace(arabicDigits(i),"5")
case "٦"
Replace(arabicDigits(i),"6")
case "٧"
Replace(arabicDigits(i),"7")
case "٨"
Replace(arabicDigits(i),"8")
case "٩"
Replace(arabicDigits(i),"9")
i=i+1
endselect
End Sub
来源:https://stackoverflow.com/questions/9785713/convert-arabic-numerical-to-english