using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoCheckSMS.Common;
namespace AutoCheckSMS
{
///
/// 生成模板
///
public class GenerateTemplate
{
private static GenerateTemplate _instacne;
public static GenerateTemplate Instance {
get {
if (_instacne == null)
{
_instacne = new GenerateTemplate();
}
return _instacne;
}
}
///
/// 生成模板
///
/// 内容
/// 返回模板内容
public string GenerateTemplates(string content)
{
try
{
StringBuilder sb = new StringBuilder();
List strList = new List();
//,=65292 。 = 12290 != 65281 《 = 12298 》= 12299
List zhAscII = new List();
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 "";
}
}
///
/// 计算字符串中子串出现的次数
///
/// 字符串
/// 子串
/// 出现的次数
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;
}
}
}