How to Convert Persian Digits in variable to English Digits Using Culture?

后端 未结 15 2116
终归单人心
终归单人心 2021-02-02 07:10

I want to change persian numbers which are saved in variable like this :

string Value=\"۱۰۳۶۷۵۱\"; 

to

string Value=\"1036751\"         


        
15条回答
  •  日久生厌
    2021-02-02 07:50

    Use this extension ,also for arabic keyboard for example : "۵", "٥" or "۴", "٤"

    static char[][] persianChars = new char[][]
        {
            "0123456789".ToCharArray(),"۰۱۲۳۴۵۶۷۸۹".ToCharArray()
        };
        static char[][] arabicChars = new char[][]
        {
            "0123456789".ToCharArray(),"٠١٢٣٤٥٦٧٨٩".ToCharArray()
        }; 
        public static string toPrevalentDigits(this string src)
        {
            if (string.IsNullOrEmpty(src)) return null;
            for (int x = 0; x <= 9; x++)
            {
                src = src.Replace(persianChars[1][x], persianChars[0][x]);
            }
            for (int x = 0; x <= 9; x++)
            {
                src = src.Replace(arabicChars[1][x], arabicChars[0][x]);
            }
            return src;
        } 
    

提交回复
热议问题