csharp:search and Compare string
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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 | /// <summary> /// 涂聚文 /// 2011 捷为工作室 /// 缔友计算机信息技术有限公司 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Page_Load( object sender, EventArgs e) { this .Page.Title = "字符串操作" ; StringBuilder search = new StringBuilder(); //查找以a字母至多5个字符放在匹配字边界 string text = @"This comprehensive compendium provides a broad and thorough investigation of all aspects of programming with asp.net. entity revised and updated for the 3.5 release of .net, this book will give you the information you need to master asp.net and build a dynamic, successful,enterprsie web application." ; string pattern = @"\ba" ; //\b字边界,\B 不是字边界的位界 这是特殊字符或转义序列 MatchCollection matches = Regex.Matches(text, pattern, RegexOptions.IgnoreCase); //WriteMatches(text, matches); Response.Write( "Original text was:" + text + "<br/>" ); Response.Write( "No.of matches:" + matches.Count.ToString() + "<br/>" ); foreach (Match nextMatch in matches) { int Index = nextMatch.Index; string result = nextMatch.ToString(); int charsBefore = (Index < 5) ? Index : 5; int formEnd = text.Length - Index - result.Length; int charsAfter = (formEnd < 5) ? formEnd : 5; int charsToDisplay = charsBefore + charsAfter + result.Length; //Console.WriteLine("index:{0},\t string:{i},\t {2}", Index, result, text.Substring(Index - charsBefore, charsToDisplay)); Response.Write( "Index:" + Index); Response.Write( ", String:" + result); Response.Write( ",..." + text.Substring(Index - charsBefore, charsToDisplay).Replace( "a" , "<font color=red>a</font>" ) + "...<br/>" ); search.Append(text.Substring(Index - charsBefore, charsToDisplay).Replace( "a" , "<font color=red>a</font>" )); } #region 得出结果 /* No.of matches:10 Index:39, String:a,ides a broa Index:47, String:a,road and th Index:77, String:a,n of all as Index:81, String:a, all aspect Index:109, String:a,with asp.ne Index:133, String:a,ised and up Index:233, String:a,ster asp.ne Index:241, String:a,.net and bu Index:251, String:a,uild a dyna Index:288, String:a, web applic */ #endregion Response.Write(search.ToString()); Response.Write( "...<br/>" ); Response.Write(SeachText(text, "a" ,5, pattern)); } /// <summary> /// 搜索文章内容得到搜索关键字截取显示 /// </summary> /// <param name="text"></param> /// <param name="strword"></param> /// <returns></returns> private string SeachText( string text, string strword, int num, string pattern) { StringBuilder search = new StringBuilder(); //string text = @"This comprehensive compendium provides a broad and thorough investigation of all aspects of programming with asp.net. entity revised and updated for the 3.5 release of .net, this book will give you the information you need to master asp.net and build a dynamic, successful,enterprsie web application."; //string pattern = @"\ba";//\b字边界,\B 不是字边界的位界 这是特殊字符或转义序列 MatchCollection matches = Regex.Matches(text, pattern, RegexOptions.IgnoreCase); //Response.Write("Original text was:" + text + "<br/>"); //Response.Write("No.of matches:" + matches.Count.ToString() + "<br/>"); foreach (Match nextMatch in matches) { int Index = nextMatch.Index; string result = nextMatch.ToString(); int charsBefore = (Index < num) ? Index : num; int formEnd = text.Length - Index - result.Length; int charsAfter = (formEnd < num) ? formEnd : num; int charsToDisplay = charsBefore + charsAfter + result.Length; //Console.WriteLine("index:{0},\t string:{i},\t {2}", Index, result, text.Substring(Index - charsBefore, charsToDisplay)); //Response.Write("Index:" + Index); //Response.Write(", String:" + result); //Response.Write(",..." + text.Substring(Index - charsBefore, charsToDisplay).Replace(strword, "<font color=red>a</font>") + "...<br/>"); string reword = "<span style='color:#FF0000;'>" + strword + "</span>" ; // "<font color=red>" + strword + "</font>"; search.Append(text.Substring(Index - charsBefore, charsToDisplay).Replace(strword, reword)); } return search.ToString(); } /// <summary> /// 首先是前台调用:strBuilder.Append ( GetLogExtract ( LogText, WordNum, LogUrl ) ); /// </summary> /// <param name="LogText"></param> /// <param name="WordNum"></param> /// <param name="LogUrl"></param> /// <returns></returns> public string GetLogExtract ( string LogText, int WordNum, string LogUrl ) { string LogExtract=LogText; if ( LogText.IndexOf ( "#此前内容作为摘要#" )>0 ) { LogExtract = LogText.Substring ( 0, LogText.IndexOf ( "#此前内容作为摘要#" ) ) + "……<a href=\"" +LogUrl+ "\">阅读全文>>></a>" ; } else { int Contentlen = GetStrLength ( LogText ); if ( Contentlen <= WordNum ) { LogExtract = LogText; } else { if ( LogText.LastIndexOf ( "<object" ) >0 || LogText.LastIndexOf ( "<OBJECT" ) >0 ) { if ( WordNum <100 ) { LogExtract= "" ; } else { LogExtract = WipeOffTableHTML ( LogText ); } } else { LogExtract = InterceptStr (WipeOffTableHTML ( LogText ), WordNum+100 ); if ( LogExtract.LastIndexOf ( "<p" )>0 && (LogExtract.Length - LogExtract.LastIndexOf ( "<p" ))<400 ) { LogExtract = LogExtract.Substring ( 0, LogExtract.LastIndexOf ( "<p" ) -1 ); } else if ( LogExtract.LastIndexOf ( "<img" )>0 && (LogExtract.Length - LogExtract.LastIndexOf ( "<img" ))<400 ) { LogExtract = LogExtract.Substring ( 0, LogExtract.LastIndexOf ( "<img" ) -1 ); } else if ( LogExtract.LastIndexOf ( "。" )>0 && (LogExtract.Length - LogExtract.LastIndexOf ( "。" ))<400 ) { LogExtract = LogExtract.Substring ( 0, LogExtract.LastIndexOf ( "。" ) ); } else if ( LogExtract.LastIndexOf ( "<br" )>0 && (LogExtract.Length - LogExtract.LastIndexOf ( "<br" ))<400 ) { LogExtract = LogExtract.Substring ( 0, LogExtract.LastIndexOf ( "<br" ) -1 ); } else if ( LogExtract.LastIndexOf ( "?" )>0 && (LogExtract.Length - LogExtract.LastIndexOf ( "?" ))<400 ) { LogExtract = LogExtract.Substring ( 0, LogExtract.LastIndexOf ( "?" ) ); } LogExtract += "……<a href=\"" +LogUrl+ "\">阅读全文>>></a>" ; } } } return LogExtract; } /// <summary> /// 去除表格HTML标记 /// </summary> /// <param name="Htmlstring"></param> /// <returns>去掉表格HTML标记后的文本</returns> public static string WipeOffTableHTML( string Htmlstring) { //删除脚本 Htmlstring = Regex.Replace(Htmlstring, @"<[^>]*?>.*?</>" , "" , RegexOptions.IgnoreCase); //删除表格HTML Htmlstring = Regex.Replace(Htmlstring, @"</?table[^>]*>" , "" , RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"</?tr[^>]*>" , "" , RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"</?td[^>]*>" , "" , RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"</?th[^>]*>" , "" , RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"</?BLOCKQUOTE[^>]*>" , "" , RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"</?tbody[^>]*>" , "" , RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"<style[^\s]*" , "" , RegexOptions.IgnoreCase); return Htmlstring; } /// <summary> /// /// </summary> /// <param name="str"></param> /// <returns></returns> public static int GetStrLength( string str) { bool WINNT_CHINESE = ( "中国" .Length == 2); if (WINNT_CHINESE) { int L, T, C; L = str.Length; T = L; for ( int i = 1; i <= L; i++) { Encoding ASCII = Encoding.ASCII; Byte[] EncodedBytes = ASCII.GetBytes(str.Substring(i - 1, 1)); C = EncodedBytes[0]; if (C < 0) C += 65536; if (C > 255) T += 1; } return T; } else { return str.Length; } } /// <summary> /// /// </summary> /// <param name="str"></param> /// <param name="length"></param> /// <returns></returns> public static string InterceptStr( string str, int length) { int x, y; str = str.Trim(); x = str.Length; y = 0; if (x >= 1) { for ( int i = 1; i <= x; i++) { Encoding ASCII = Encoding.ASCII; Byte[] EncodedBytes = ASCII.GetBytes(str.Substring(i - 1, 1)); if (EncodedBytes[0] < 0 || EncodedBytes[0] > 255) { y += 2; } else { y += 1; } if (y >= length) { str = str.Substring(0, i); break ; } } return str; } else { return "" ; } } |
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 124 125 126 127 128 129 130 131 132 133 | /// <summary> /// 去除重复的字符 /// </summary> /// <param name="source">原字符串</param> /// <returns>返回无重复的字符串</returns> private string getRemoveRepeatString( string source) { string s = string .Empty; s = Regex.Replace(source, "(?s)(.)(?=.*\\1)" , "" ); return s; } /// <summary> /// 去除重复的字符 /// </summary> /// <param name="source"></param> /// <returns></returns> private string getRemoveString( string source) { string str = string .Empty; ArrayList Alist = new ArrayList(); foreach ( char ch in source.ToCharArray()) { if (!Alist.Contains(ch)) { Alist.Add(ch); } } foreach ( object o in Alist) { str = str + o.ToString(); } return str; } /// <summary> /// 去除有分割符号重复的字串 /// </summary> /// <param name="source">原字符串</param> /// <param name="key">分割字符</param> /// <returns>返回无重复的字符串</returns> private string getRemoveArryString( string source, char key) { string str = string .Empty; Array stringArray = source.Split(key); List< string > listString = new List< string >(); //遍历删除重复项 foreach ( string eachString in stringArray) { if (!listString.Contains(eachString)) listString.Add(eachString); } //遍历输出 foreach ( string string1 in listString) //测试值 { str = str + string1 + key; } str = str.Substring(0, str.Length - 1); return str; } /// <summary> /// 去除有分割符号重复的字符重复的字串 /// </summary> /// <param name="source">原字符串</param> /// <param name="key">分割字符</param> /// <returns>返回无重复的字符串</returns> private string getRemoveArryStringR( string source, string key) { string str = string .Empty; source = Regex.Replace(source + key, @"(?:([^,]+,))(?=.*?\1)" , "" ); str = source.Substring(0, source.Length - 1); return str; } /// <summary> /// 去除有分割符号重复的字串 /// </summary> /// <param name="source">原字符串</param> /// <param name="key">分割字符</param> /// <returns>返回无重复的字符串</returns> private string getRemoveArryStringJ( string source, char key) { string str = string .Empty; str = string .Join( "," , source.Split(key).Distinct().ToArray()); return str; } /// <summary> /// 去除有分割符号重复的字串 /// </summary> /// <param name="source">原字符串</param> /// <param name="key">分割字符</param> /// <returns>返回无重复的字符串</returns> private string getRemoveArryStringD( string source, char key) { string str = string .Empty; Array stringArray = source.Split(key); List< string > listString = new List< string >(); foreach ( string eachString in stringArray) { listString.Add(eachString); } listString = listString.Distinct().ToList(); str = String.Join( "," , listString.ToArray()); return str; } /// <summary> /// 去除有分割符号重复的字串 /// LINQ /// </summary> /// <param name="source">原字符串</param> /// <param name="key">分割字符</param> /// <returns>返回无重复的字符串</returns> private string getRemoveArryStringL( string source, char key) { string str = string .Empty; Array stringArray = source.Split(key); List< string > listString = new List< string >(); foreach ( string eachString in stringArray) { listString.Add(eachString); } List< string > strDistinct = new List< string >(); IEnumerable< string > whoLoggedIn = listString.Where(logEntry => logEntry.Length < 15).Distinct(); //logEntry.Length<10 foreach ( string who in whoLoggedIn) { strDistinct.Add(who); } str = String.Join( "," , strDistinct.ToArray()); return str; } |
http://blogs.msdn.com/b/kcwalina/archive/2004/06/22/162533.aspx
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 | /// <summary> /// /// </summary> public class Person { private int _ID; private string _Name; private int _Age; public Person( int ID, string Name, int Age) { _ID = ID; _Name = Name; _Age = Age; } public int ID { set { _ID = value; } get { return _ID; } } public string Name { set { _Name = value; } get { return _Name; } } public int Age { set { _Age = value; } get { return _Age; } } public override string ToString() { return string .Format( "ID:{0},Name:{1},Age:{2}" , _ID, _Name, _Age); } }
string str = string .Empty; List< string > strout = new List< string >(); List<Person> lstPerson = new List<Person>(); lstPerson.Add( new Person(1, "puma" , 10)); lstPerson.Add( new Person(2, "F6 Team" , 20)); lstPerson.Add( new Person(3, "ASP.NET" , 30)); lstPerson.Add( new Person(4, "Dotblogs" , 40)); //List<T>.Find() //找出Name='puma'的Person str= "找出Name='puma'的Person→ " ; str=str+lstPerson.Find( delegate (Person p) { return p.Name == "puma" ; }).ToString() + "<p>" ; //List<T>.FindAll() //找出Age>10的數目 str= "找出Age>10的數目→ " ; str=lstPerson.FindAll( delegate (Person p) { return p.Age > 10; }).Count.ToString() + "<p>" ; //List<T>.Exists() //檢查Name='F6'是否存在 str= "檢查Name='F6'是否存在→ " ; str=lstPerson.Exists( delegate (Person p) { return p.Name == "F6" ; }).ToString() + "<p>" ; //List<T>.Sort() //依Name升冪排序 str= "<p>依Name升冪排序↑<br/>" ; lstPerson.Sort( delegate (Person p1, Person p2) { return Comparer< string >.Default.Compare(p1.Name, p2.Name); }); foreach (Person p in lstPerson) { strout.Add(p.ToString() + "<br/>" ); } str = str + String.Join( "," , strout.ToArray()); //List<T>.Sort() //依Name降冪排序 str= "<p>依Name降冪排序↓<br/>" ; lstPerson.Sort( delegate (Person p1, Person p2) { return Comparer< string >.Default.Compare(p2.Name, p1.Name); }); foreach (Person p in lstPerson) { strout.Add(p.ToString() + "<br/>" ); } str = str + String.Join( "," , strout.ToArray()); |
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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | /// <summary> /// /// </summary> public Form1() { InitializeComponent(); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_Load( object sender, EventArgs e) { string a = @"E:\geovindu\k\国家\dd\FName.jpg" ; //或者 a=System.IO.Path.GetFileName(a); string []arr=a.Split( '\\' ); a=arr[arr.Length-1].Split( '.' )[0]; //MessageBox.Show("file:" + a); List< string > sk = new List< string >(); string str1= "我**是*****涂*****聚*****文*****啊" ; string [] str2 = System.Text.RegularExpressions.Regex.Split(str1, @"[*]+" ); foreach ( string i in str2) sk.Add(i.ToString()); string sks = string .Empty; string skf = string .Empty; int c = 1; foreach ( var ss in sk) { if (c >= 2) { skf = skf + "," + ss; } else { skf = skf + ss; } c++; } this .textBox2.Text = getStrWhere(skf, "geovindu" , ',' ); sks = getListWhere(sk, "geovindu" ); //MessageBox.Show(sks); this .textBox1.Text = sks; } /// <summary> /// /// </summary> /// <param name="filedlist"></param> /// <param name="strkey"></param> /// <param name="stroption"></param> /// <returns></returns> private string getStrWhere( string filedlist, string strkey, string stroption) { string str = string .Empty; if (! string .IsNullOrEmpty(strkey)) { int i = 1; string [] resultString = Regex.Split(filedlist, stroption, RegexOptions.IgnoreCase); string [] str2 = System.Text.RegularExpressions.Regex.Split(filedlist, @"[,]+" ); //多个,连在一起也可以分割 string [] split = filedlist.Split( new Char[] { ',' }); foreach ( var s in split) { if (i >= 2) { str = str + " or " + s + " like '%" + strkey + "%'" ; } else { str = str + " " + s + " like '%" + strkey + "%'" ; } i++; } } return str; } /// <summary> /// /// </summary> /// <param name="filedlist"></param> /// <param name="strkey"></param> /// <returns></returns> private string getStrWhere( string filedlist, string strkey, char stroption) { string str = string .Empty; if (! string .IsNullOrEmpty(strkey)) { int i = 1; string [] resultString = Regex.Split(filedlist, "," , RegexOptions.IgnoreCase); string [] str2 = System.Text.RegularExpressions.Regex.Split(filedlist, @"[,]+" ); //多个,连在一起也可以分割 string [] split = filedlist.Split( new Char[] { stroption }); foreach ( var s in split) { if (i >= 2) { str = str + " or " + s + " like '%" + strkey + "%'" ; } else { str = str + " " + s + " like '%" + strkey + "%'" ; } i++; } } return str; } /// <summary> /// 2016-05-26 /// geovindu /// </summary> /// <param name="fileidlist"></param> /// <param name="strkye"></param> /// <returns></returns> private string getListWhere(List< string > fileidlist, string strkey) { int i = 1; string str = string .Empty; foreach ( var s in fileidlist) { if (i >= 2) { str = str + " or " + s + " like '%" + strkey + "%'" ; } else { str = str + " " + s + " like '%" + strkey + "%'" ; } i++; } return str; } } |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2009-02-07 C#2.0 print winform 打印窗体数据试试