Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#.Regex.replace忽略不起作用的大小写_C#_Asp.net_Regex_Replace_Ignore Case - Fatal编程技术网

c#.Regex.replace忽略不起作用的大小写

c#.Regex.replace忽略不起作用的大小写,c#,asp.net,regex,replace,ignore-case,C#,Asp.net,Regex,Replace,Ignore Case,围绕这个问题有很多问题,但没有一个能解决我的问题。我有一个SQL server数据库作为数据源,一个输入文本框和一个搜索按钮。输入文本并按下搜索按钮后,将显示包含搜索文本的行的下拉列表。用户选择要查看的行,该信息将显示在gridview中。(返回1行) 我想突出显示搜索的文本。这就是我所拥有的,它应该可以工作,但我不明白为什么它不能: foreach (GridViewRow row in searchTextGridView2.Rows) { strin

围绕这个问题有很多问题,但没有一个能解决我的问题。我有一个SQL server数据库作为数据源,一个输入文本框和一个搜索按钮。输入文本并按下搜索按钮后,将显示包含搜索文本的行的下拉列表。用户选择要查看的行,该信息将显示在gridview中。(返回1行)

我想突出显示搜索的文本。这就是我所拥有的,它应该可以工作,但我不明白为什么它不能:

foreach (GridViewRow row in searchTextGridView2.Rows)
        {
            string text = searchText_txt.Text; //Text that was entered in the search text field
            int length = searchTextGridView2.Columns.Count; //Number of Columns on the grid
            for (int i = 0; i < length; i++) //loop through each column
            {
                string newText = row.Cells[i].Text.ToString(); //Get the text in the cell
                if (newText.Contains(text)) //If the cell text contains the search text then do this
                {
                    string highlight = "<span style='background-color:yellow'>" + text + "</span>";
                    string replacedText = Regex.Replace(newText, text, highlight, RegexOptions.IgnoreCase);
                    row.Cells[i].Text = replacedText;
                }
            }
        }
foreach(searchTextGridView2.Rows中的GridViewRow行)
{
string text=searchText\u txt.text;//在搜索文本字段中输入的文本
int length=searchTextGridView2.Columns.Count;//网格上的列数
for(int i=0;i
上述代码位于下拉列表中所选项目已更改的事件内。
如果我搜索“索赔”,它将突出显示该单词的所有实例,但如果我搜索“索赔”,它只突出显示大写字母“C”的单词。您的问题不在于
Replace()
方法,而在于
Contains()
方法

无论何时对字符串调用
Contains()
,它都将执行区分大小写的比较,因此以下行将始终返回
false

"Some Claims".Contains("claims");
为了克服这一问题,应使用以下方法:

for(int i=0;i=0
{
字符串突出显示=“$0”;
string replacedText=Regex.Replace(newText、text、highlight、RegexOptions.IgnoreCase);
row.Cells[i].Text=replacedText;
}
}

您的问题不在于
Replace()
方法,而是
Contains()
方法

无论何时对字符串调用
Contains()
,它都将执行区分大小写的比较,因此以下行将始终返回
false

"Some Claims".Contains("claims");
为了克服这一问题,应使用以下方法:

for(int i=0;i=0
{
字符串突出显示=“$0”;
string replacedText=Regex.Replace(newText、text、highlight、RegexOptions.IgnoreCase);
row.Cells[i].Text=replacedText;
}
}

这听起来似乎应该有效,但它没有:(虽然使用了.Contains,但它确实为我指明了正确的方向。我使用了
int index=newText.IndexOf(text,StringComparison.CurrentCultureIgnoreCase)
相反,这样做效果很好。。ty@KieranQuinn,我的错;我确实想指出那个特定的重载,但完全忘记了。我已经编辑了答案。@Rawling,你确定吗?MSDN没有提到过这样的重载,Visual Studio intellisense也没有。@repier我很抱歉,我说的完全是废话。这听起来像是应该的d工作,但它没有:(它确实为我指明了正确的方向,尽管使用了.Contains。我使用了
int index=newText.IndexOf(text,StringComparison.CurrentCultureIgnoreCase)
相反,这样做效果很好。。ty@KieranQuinn,我的错;我确实想指出那个特定的过载,但完全忘记了。我已经编辑了答案。@Rawling,你确定吗?MSDN没有提到过这样的过载,Visual Studio intellisense也没有。@RePierre我很抱歉,我说的完全是废话。我想指出t、 即使修复了区分大小写的
包含
问题,如果搜索
x
,代码仍会(1)将
x
的实例替换为
x
,以及(2)如果搜索
,请将所有内容替换为
。是的,现在正在运行。我想我必须获得搜索项的长度,在单元格中找到索引,然后使用这些变量将在单元格中找到的文本复制到新变量,以便在切换
时维护(1)的大小写。“”+text+“”
“$0”
$0
告诉正则表达式使用它在替换中匹配的文本。对于(2),您可以在输入上使用`Regex.Escape1。我想指出的是,即使修复了区分大小写的
包含
问题,您的代码仍然会(1)如果搜索
x
,则将
x
的实例替换为
x
,以及(2)如果搜索
,请将所有内容替换为
。是的,现在正在运行。我想我必须获得搜索项的长度,在单元格中找到索引,然后使用这些变量将在单元格中找到的文本复制到新变量,以便在切换
时维护(1)的大小写。“”+text+“”
“$0”
$0
告诉正则表达式使用替换中匹配的文本。对于(2),可以在输入上使用`Regex.Escape1。