我有这段代码从DGV中选择一行。%“。search。”%in C#
if (row.Cells[1].Value.ToString().Equals(searchValue))
{
row.Selected = true;
break;
}
有没有像
%".$searchvalue."%
从SQL在C#中使用任何东西,所以它会发现貌似出入口不仅准确的呢?如果你想找出你可以使用StartsWith
像下面的字符串只有entrances
我有这段代码从DGV中选择一行。%“。search。”%in C#
if (row.Cells[1].Value.ToString().Equals(searchValue))
{
row.Selected = true;
break;
}
有没有像
%".$searchvalue."%
从SQL在C#中使用任何东西,所以它会发现貌似出入口不仅准确的呢?如果你想找出你可以使用StartsWith
像下面的字符串只有entrances
使用string.Contains代替的Equals
if (row.Cells[1].Value.ToString().Contains(searchValue))
{
row.Selected = true;
break;
}
非常感谢Gilad! – Vvisions
。
if (row.Cells[1].Value.ToString().StartsWith(searchValue))
{
row.Selected = true;
break;
}
如果你想找出字符串只有endings
你可以使用EndsWith
像下面。
if (row.Cells[1].Value.ToString().EndsWith(searchValue))
{
row.Selected = true;
break;
}
可以在上述情况下使用contains
如果你不是那么担心性能。
StartsWith将仅查找第一个单词的第一个字符或将查找任何单词的第一个字符? 如果我使用包含什么性能影响? – Vvisions
它只查找第一个单词的第一个字符。 'Contains'使用不同的算法,它需要计算比'StartsWith'或'EndsWith'更多的组合。因此它用于我们不知道我们所需的字符串匹配器在哪里的情况。如果你确定你需要搜索什么,并且它在开始或结束时出现,那么你可以使用这些。否则'Contains'不是一个不错的选择。 – Venky
如果你想检查每个单词的第一个字符,那么你需要用'space'分割字符串,并循环遍历每一个单词以检查它是否以'匹配字符串'开头。 – Venky
在一般情况下,如果你想模仿 SQL LIKE
建设可以尝试正则表达式:
public static bool Like(String value, String like) {
if (String.IsNullOrEmpty(like) || String.IsNullOrEmpty(value))
return false; // or throw exception
String pattern = "^" + Regex.Escape(like).Replace("%", ".*").Replace("_", ".") + "$";
return Regex.IsMatch(value, pattern);
}
....
String source = "abcdef";
// true
bool result = Like(source, "%b_d%");
你的情况
if (Like(row.Cells[1].Value.ToString(), searchValue)) { ... }
可能的重复[如何在Linq中执行SQL Like%?](http://stackoverflow.com/questions/835790/how-to-do-sql-like-in-linq) –