C#版中文数字转阿拉伯数字

原本是群里某位大哥发的JavaScript版,因为项目里需要,改写成了C#版的,希望能对需要的人帮点忙哈~~~
+展开
-C#
       /// <summary>
        /// 将中文数字替换为阿拉伯数字
       /// </summary>
       /// <param name="word"></param>
       /// <returns></returns>
     public static string WordToNumber(string word)
       {
            string e = "([零一二三四五六七八九十百千万亿])+";
           MatchCollection mc = Regex.Matches(word, e);
           foreach(Match m in mc)
           {
               word = word.Replace(m.Value, Word2Number(m.Value));
           }
           return word;
       }
        private static string Word2Number(string w)
       {
           if(w == "")
             return w;

            string e = "零一二三四五六七八九";
            string[] ew = new string[]{"十""百""千"};
            string ewJoin = "十百千";
            string[] ej = new string[]{"万""亿"};

            string rss = "^([" + e + ewJoin + "]+" + ej[1] + ")?([" + e
                + ewJoin + "]+" +ej[0] + ")?([" + e + ewJoin + "]+)?$";
            string[] mcollect = Regex.Split(w, rss);
            if(mcollect.Length < 4)
                return w;
            return (
                Convert.ToInt64(foh(mcollect[1])) * 100000000 +
               Convert.ToInt64(foh(mcollect[2])) * 10000 +
                Convert.ToInt64(foh(mcollect[3]))
                ).ToString();
        }

        private static int foh(string str)
        {
            string e = "零一二三四五六七八九";
            string[] ew = new string[]{"十""百""千"};
            string[] ej = new string[]{"万""亿"};

            int a = 0;
            if(str.IndexOf(ew[0]) == 0)
                a = 10;
            str = Regex.Replace(str, e[0].ToString(), "");

            if(Regex.IsMatch(str, "([" + e + "])$"))
            {
                a += e.IndexOf(Regex.Match(str, "([" + e + "])$").Value[0]);
            }

            if(Regex.IsMatch(str, "([" + e + "])" + ew[0]))
            {
                a += e.IndexOf(Regex.Match(str, "([" + e + "])" + ew[0]).Value[0]) * 10;
            }

            if(Regex.IsMatch(str, "([" + e + "])" + ew[1]))
            {
                a += e.IndexOf(Regex.Match(str, "([" + e + "])" + ew[1]).Value[0]) * 
            }

            if(Regex.IsMatch(str, "([" + e + "])" + ew[2]))
            {
                a += e.IndexOf(Regex.Match(str, "([" + e + "])" + ew[2]).Value[0]) * 10
            }
            return a;
        }

加支付宝好友偷能量挖...


评论(0)网络
阅读(121)喜欢(0)Asp.Net/C#/WCF