wzp
2021-06-08 85e2382799b60c823e3613256605fdde928b0fd6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            
 
 
 
            //string str1 = "【爱育幼童新沂校区】您好,计次卡卡号0005123046,本次充值150,余额155,谢谢惠顾。";
            string str1 = "尊敬的重庆巴源建设投资有限公司客户(户号:30102281703,地址:凤华康城凤林路809号5栋14-6),202105月抄表读数为581-591,水量10立方米,水费43元,请在本月25日前缴纳.详询966886【东部水务】";
            //string str1 = "1231231232jsdfsdfsdfasf";
 
 
 
            //使用ASCII码提取
            StringBuilder sb = new StringBuilder();
            List<string> strList = new List<string>();
            //,=65292 。 = 12290 != 65281 《 = 12298 》= 12299
            List<int> zhAscII = new List<int>();
            zhAscII.Add(65292);
            zhAscII.Add(12290);
            zhAscII.Add(65281);
            zhAscII.Add(12298);
            zhAscII.Add(12299);
            zhAscII.Add(65306);
            foreach (char c in str1)
            {
                //Console.WriteLine($"{c} ="+ Convert.ToInt32(c));
                if ((Convert.ToInt32(c) >= 32 && Convert.ToInt32(c) <= 122)||zhAscII.Contains(Convert.ToInt32(c)))
                {
                    sb.Append(c);
                }
                else
                {
                    if (sb.Length > 0)
                    {
                        strList.Add(sb.ToString());
                        sb.Remove(0, sb.Length);
                    }
                }
            }
 
            //最后结尾的时候含有ASCII码里面设定的值
            if (sb.Length > 0)
            {
                strList.Add(sb.ToString());
                sb.Remove(0, sb.Length);
            }
 
            string[] arr = strList.ToArray();
            arr = arr.OrderByDescending(o=>o.Length).ToArray();
 
            strList = arr.ToList();
            //strList.Sort();
            //strList.Reverse(); 这个不行。。需要根据string 长度排序
            string result = str1;
            foreach (string s in strList)
            {
                result = result.Replace(s,"*");
            }
 
            int count = SubstringCount(result,"*");
            if (count == result.Length)
            {
                //todo,不入库
            }
 
            Console.WriteLine(result);
 
            Console.WriteLine($"*出现次数{count},字符串长度:{result.Length}");
            Console.ReadKey();
        }
 
        /// <summary>
        /// 计算字符串中子串出现的次数
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="substring">子串</param>
        /// <returns>出现的次数</returns>
        static int SubstringCount(string str, string substring)
        {
            if (str.Contains(substring))
            {
                string strReplaced = str.Replace(substring, "");
                return (str.Length - strReplaced.Length) / substring.Length;
            }
 
            return 0;
        }
    }
}