Is there a function to compare a string to a larger string?
This question already has an answer here:
If your search is case-sensitive, you can use Contains
:
var name = "John Smith";
if(name.Contains("John"))
{
// Name contains John
}
Or, to get all the names that contain John:
var names = new string[] {"John Smith", "Bob Smith", "Frank"};
var results = names.Where(n => n.Contains("John"));
If you want to ignore the case, you can convert both strings to lowercase:
var results = names.Where(n => n.ToLower().Contains("john"));
You could also implement your own case-insensitive Contains
function as an extention method:
public static bool Contains(this string value, string substr, StringComparison c)
{
return value.IndexOf(substr, c) >= 0;
}
Then use:
var results = names.Where(n => n.Contains("John", StringComparison.OrdinalIgnoreCase));
IList<string> lst = new List<string>();
lst.add("John Smith");
lst.add("Elton Johns");
lst.add("mark");
lst.add("jones");
现在要获取包含"John"
的名字
var resultList = lst.Where(x => x.Contains("John")).ToList();
使用System.Linq
private static List<String> GetNames(List<string> names ,string name)
{
return names.Where(x => x.ToLower().Contains(name.ToLower())).ToList();
}
链接地址: http://www.djcxy.com/p/13098.html
上一篇: 禁用我的关键字搜索的区分大小写
下一篇: 有没有函数比较一个字符串与较大的字符串?