using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
using AutoCheckSMS.Common;
|
|
namespace AutoCheckSMS
|
{
|
/// <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)
|
{
|
result = result.Replace(s, "*");
|
}
|
|
int count = SubstringCount(result, "*");
|
if (count == result.Length)
|
{
|
//todo,不入库,全部都是* ,不进行下一步的处理
|
return "";
|
}
|
|
return result;
|
}
|
catch (Exception ex)
|
{
|
Log4netService.Error($"生成模板异常:{ex.Message}");
|
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;
|
}
|
}
|
}
|