原本是群里某位大哥发的JavaScript版,因为项目里需要,改写成了C#版的,希望能对需要的人帮点忙哈~~~
1 /// <summary>
2 /// 将中文数字替换为阿拉伯数字
3 /// </summary>
4 /// <param name="word"></param>
5 /// <returns></returns>
6 public static string WordToNumber(string word)
7 {
8 string e = "([零一二三四五六七八九十百千万亿])+";
9 MatchCollection mc = Regex.Matches(word, e);
10
11 foreach(Match m in mc)
12 {
13 word = word.Replace(m.Value, Word2Number(m.Value));
14 }
15 return word;
16 }
17
18 private static string Word2Number(string w)
19 {
20 if(w == "")
21 return w;
22
23 string e = "零一二三四五六七八九";
24 string[] ew = new string[]{"十", "百", "千"};
25 string ewJoin = "十百千";
26 string[] ej = new string[]{"万", "亿"};
27
28 string rss = "^([" + e + ewJoin + "]+" + ej[1] + ")?([" + e
29 + ewJoin + "]+" +ej[0] + ")?([" + e + ewJoin + "]+)?$";
30 string[] mcollect = Regex.Split(w, rss);
31 if(mcollect.Length < 4)
32 return w;
33 return (
34 Convert.ToInt64(foh(mcollect[1])) * 100000000 +
35 Convert.ToInt64(foh(mcollect[2])) * 10000 +
36 Convert.ToInt64(foh(mcollect[3]))
37 ).ToString();
38 }
39
40 private static int foh(string str)
41 {
42 string e = "零一二三四五六七八九";
43 string[] ew = new string[]{"十", "百", "千"};
44 string[] ej = new string[]{"万", "亿"};
45
46 int a = 0;
47 if(str.IndexOf(ew[0]) == 0)
48 a = 10;
49 str = Regex.Replace(str, e[0].ToString(), "");
50
51 if(Regex.IsMatch(str, "([" + e + "])$"))
52 {
53 a += e.IndexOf(Regex.Match(str, "([" + e + "])$").Value[0]);
54 }
55
56 if(Regex.IsMatch(str, "([" + e + "])" + ew[0]))
57 {
58 a += e.IndexOf(Regex.Match(str, "([" + e + "])" + ew[0]).Value[0]) * 10;
59 }
60
61 if(Regex.IsMatch(str, "([" + e + "])" + ew[1]))
62 {
63 a += e.IndexOf(Regex.Match(str, "([" + e + "])" + ew[1]).Value[0]) * 100;
64 }
65
66 if(Regex.IsMatch(str, "([" + e + "])" + ew[2]))
67 {
68 a += e.IndexOf(Regex.Match(str, "([" + e + "])" + ew[2]).Value[0]) * 1000;
69 }
70 return a;
71 }
2 /// 将中文数字替换为阿拉伯数字
3 /// </summary>
4 /// <param name="word"></param>
5 /// <returns></returns>
6 public static string WordToNumber(string word)
7 {
8 string e = "([零一二三四五六七八九十百千万亿])+";
9 MatchCollection mc = Regex.Matches(word, e);
10
11 foreach(Match m in mc)
12 {
13 word = word.Replace(m.Value, Word2Number(m.Value));
14 }
15 return word;
16 }
17
18 private static string Word2Number(string w)
19 {
20 if(w == "")
21 return w;
22
23 string e = "零一二三四五六七八九";
24 string[] ew = new string[]{"十", "百", "千"};
25 string ewJoin = "十百千";
26 string[] ej = new string[]{"万", "亿"};
27
28 string rss = "^([" + e + ewJoin + "]+" + ej[1] + ")?([" + e
29 + ewJoin + "]+" +ej[0] + ")?([" + e + ewJoin + "]+)?$";
30 string[] mcollect = Regex.Split(w, rss);
31 if(mcollect.Length < 4)
32 return w;
33 return (
34 Convert.ToInt64(foh(mcollect[1])) * 100000000 +
35 Convert.ToInt64(foh(mcollect[2])) * 10000 +
36 Convert.ToInt64(foh(mcollect[3]))
37 ).ToString();
38 }
39
40 private static int foh(string str)
41 {
42 string e = "零一二三四五六七八九";
43 string[] ew = new string[]{"十", "百", "千"};
44 string[] ej = new string[]{"万", "亿"};
45
46 int a = 0;
47 if(str.IndexOf(ew[0]) == 0)
48 a = 10;
49 str = Regex.Replace(str, e[0].ToString(), "");
50
51 if(Regex.IsMatch(str, "([" + e + "])$"))
52 {
53 a += e.IndexOf(Regex.Match(str, "([" + e + "])$").Value[0]);
54 }
55
56 if(Regex.IsMatch(str, "([" + e + "])" + ew[0]))
57 {
58 a += e.IndexOf(Regex.Match(str, "([" + e + "])" + ew[0]).Value[0]) * 10;
59 }
60
61 if(Regex.IsMatch(str, "([" + e + "])" + ew[1]))
62 {
63 a += e.IndexOf(Regex.Match(str, "([" + e + "])" + ew[1]).Value[0]) * 100;
64 }
65
66 if(Regex.IsMatch(str, "([" + e + "])" + ew[2]))
67 {
68 a += e.IndexOf(Regex.Match(str, "([" + e + "])" + ew[2]).Value[0]) * 1000;
69 }
70 return a;
71 }
想做些令人惊奇的东西出来