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 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 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(); } /// /// 计算字符串中子串出现的次数 /// /// 字符串 /// 子串 /// 出现的次数 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; } } }