/// <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 "";
}
}
 /// <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

http://www.dotblogs.com.tw/puma/archive/2009/05/28/asp.net-generic-list-sort-find-findall-exsit.aspx#12190

/// <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());

  

        /// <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;
}
}

  

csharp:search and Compare string的更多相关文章

  1. 用java8重写Arrays.sort(oldWay, new Comparator<String>(){@Override public int compare(String s1, String s2)});

    参考https://www.liaoxuefeng.com/article/001411306573093ce6ebcdd67624db98acedb2a905c8ea4000/ Java 8终于引进 ...

  2. 1,字符是否为空,2,比较两个字符大小。String.Compare(String, String)。string.IsNullOrEmpty(string)

    1, String.Compare 方法 (String, String) 比较两个指定的 String 对象. 值 条件 小于零 strA 小于 strB. 零 strA 等于 strB. 大于零 ...

  3. 温故知新,CSharp遇见字符串比较(String Comparison),更佳科学的比较字符串

    背景 在C#中,我们经常会遇到需要比较字符串的场景,有时候甚至因为外部输入的不确定性,我们需要忽略大小写来进行比较,以达到判断业务的述求. 对字符串用法的建议 使用.NET进行开发时,请遵循以下简要建 ...

  4. elastic search 日期为string类型导致视图无法展示时间的解决办法

    尝试将结构化的json数据发送到es(elastic search)上,然后创建视图,这样就能以小时维度查看数据,直接使用post发送到es后,创建索引,结果提示 没有date类型的字段(field) ...

  5. csharp: Double Convert To String

    /// <summary> /// /// </summary> /// <param name="fl"></param> /// ...

  6. Elastic Search中Query String常见语法

    1 搜索所有数据timeout参数:是超时时长定义.代表每个节点上的每个shard执行搜索时最多耗时多久.不会影响响应的正常返回.只会影响返回响应中的数据数量.如:索引a中,有10亿数据.存储在5个s ...

  7. 深入解析字符串的比较方法:“==”操作符;String.Equals方法;String.Compare方法;String.CompareOrdinal方法。

    1:要判断2个字符串变量是否相等,最高效的方法是看它们是否指向相同的内存地址.前面使用RefernceEquals方法来比较.如果2个变量指向的是不同的内存地址,那么就需要逐字符的比较2个字符串的变量 ...

  8. C#基础知识之字符串比较方法:“==”操作符;RefernceEquals;String.Equals方法;String.Compare方法;String.CompareOrdinal方法。

    一.“==”操作符:String.Equals:ReferenceEquals 方法 1.在编程中实际上我们只需要这两种比较,c#中类型也就这两种 (1)值类型的比较:一般我们就是判断两个值类型实例各 ...

  9. String.Compare 方法 (String, Int32, String, Int32, Int32)

    String.Compare 方法 (String, Int32, String, Int32, Int32) 对两个指定的 String 对象的子字符串进行比较,并返回一个指示二者在排序顺序中的相对 ...

随机推荐

  1. leetcode-849-到最近的人的最大距离

    题目描述: 在一排座位( seats)中,1 代表有人坐在座位上,0 代表座位上是空的. 至少有一个空座位,且至少有一人坐在座位上. 亚历克斯希望坐在一个能够使他与离他最近的人之间的距离达到最大化的座 ...

  2. linux下不同颜色文件的性质

    绿色文件: 可执行文件,可执行的程序 红色文件:压缩文件或者包文件 蓝色文件:目录 白色文件:一般性文件,如文本文件,配置文件,源码文件等 浅蓝色文件:链接文件,主要是使用ln命令建立的文件 红色闪烁 ...

  3. Scala 中使用 akka system 的 scheduler 的例子

    这是在scala控制台直接执行的例子.   import akka.actor._ import scala.concurrent.duration._ import scala.concurrent ...

  4. android:activity知识点

    一.活动流程 1.创建活动 public class firstActivity extends Activity{} 2.创建布局 新建first_layout.xml文件 3.注册活动 在andr ...

  5. Mac 10.12原生方法对NTFS分区进行读写的配置

    说明:不一定有效,最简单的方法就是不用NTFS,直接FAT32,对于大文件就用切割. 方法: 1.确定U盘名称 diskutil list ls /Volumes/ 2.比如我找到的U盘名称为Unti ...

  6. Mac下一台电脑管理多个SSH KEY(转)

    一.关于ssh是什么? http://www.ruanyifeng.com/blog/2011/12/ssh_remote_login.html 二.需求: 一台电脑上(Mac os)管理多个ssh ...

  7. html5+js+.Net的即时多人聊天

            今天看了下websocket的知识,了解到这是html5新增的特性,主要用于实时web的通信.之前客户端获取服务端的数据,是通过客户端发出请求,服务端进行响应的模式,或者通过ajax每 ...

  8. (转)一张图学会Dockerfile

    原文:http://blog.51cto.com/kusorz/1942816 前言 Dockerfile是非常容易学的,和SHELL相比那简单的太多了. Dockerfile是为快速构建docker ...

  9. TortoiseGit学习系列之TortoiseGit基本操作将提交到本地的项目推送到在线仓库(图文详解)

    前面博客 TortoiseGit学习系列之TortoiseGit基本操作克隆项目(图文详解) TortoiseGit学习系列之TortoiseGit基本操作修改提交项目(图文详解) TortoiseG ...

  10. Volley 源码解析(转)

    项目:Volley,分析者:grumoon,校对者:Trinea 本文为 Android 开源项目源码解析 中 Volley 部分项目地址:Volley,分析的版本:35ce778,Demo 地址:V ...