wzp
2021-08-09 826bce65ac9b1e5dae43fc06974b8b1284f1d12c
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Common
{
    /// <summary>
    /// 生成模板
    /// </summary>
    public class GenerateTemplate
    {
        private static GenerateTemplate _instacne;
        public static GenerateTemplate Instance
        {
            get
            {
                if (_instacne == null)
                {
                    _instacne = new GenerateTemplate();
                }
                return _instacne;
            }
        }
 
 
 
        /// <summary>
        /// 生成模板
        /// </summary>
        /// <param name="content">内容</param>
        /// <returns>返回模板内容</returns>
        public string GenerateTemplates(string content)
        {
            try
            {
                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 content)
                {
                    //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();
                string result = content;
                foreach (string s in strList)
                {
                    if (s.Length <= 10)
                    {
                        result = result.Replace(s, "*"); continue;
                    }
                    if (s.Length > 10 && s.Length <= 20)
                    {
                        result = result.Replace(s, "**"); continue;
                    }
                    if (s.Length > 20)
                    {
                        result = result.Replace(s, "***"); continue;
                    }
                }
 
                int count = SubstringCount(result, "*");
                if (count == result.Length)
                {
                    //todo,不入库,全部都是* ,不进行下一步的处理
                    return "";
                }
 
                return result;
            }
            catch (Exception ex)
            {
                return "";
            }
        }
 
        /// <summary>
        /// 计算字符串中子串出现的次数
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="substring">子串</param>
        /// <returns>出现的次数</returns>
        private int SubstringCount(string str, string substring)
        {
            if (str.Contains(substring))
            {
                string strReplaced = str.Replace(substring, "");
                return (str.Length - strReplaced.Length) / substring.Length;
            }
 
            return 0;
        }
    }
}