How to Convert Persian Digits in variable to Engli

2020-05-19 19:55发布

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

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

to

string Value="1036751";

How can I use easy way like culture info to do this please?

my sample code is:

List<string> NERKHCOlist = new List<string>();
NERKHCOlist = ScrappingFunction(NERKHCO, NERKHCOlist);
int NERKHCO_Price = int.Parse(NERKHCOlist[0]);//NERKHCOlist[0]=۱۰۳۶۷۵۱ 

<= So it can not Parsed it to int
And This is in my function which retun a list with persian digits inside list items

protected List<string> ScrappingFunction(string SiteAddress, List<string> NodesList)
{    
    string Price = "null";
    List<string> Targets = new List<string>();
    foreach (var path in NodesList)
    {
        HtmlNode node = document.DocumentNode.SelectSingleNode(path.ToString());//recognizing Target Node
        Price = node.InnerHtml;//put text of target node in variable(PERSIAN DIGITS)
        Targets.Add(Price);
    }
    return Targets;
}

14条回答
我命由我不由天
2楼-- · 2020-05-19 21:00
public static string ChangeNumberToEnglishNumber(string value)
    {
        string result=string.Empty;
        foreach (char ch in value)
        {

            try
            {
                double convertedChar = char.GetNumericValue(ch);
                if (convertedChar >= 0 && convertedChar <= 9)
                {
                    result += convertedChar.ToString(CultureInfo.InvariantCulture);
                }
                else
                {
                    result += ch;
                }


            }
            catch (Exception e)
            {
                result += ch;
            }

        }

        return result;
    }
查看更多
趁早两清
3楼-- · 2020-05-19 21:01

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;
    } 
查看更多
登录 后发表回答