dll*_*l32 7 .net c# regex string replace
老:
private string Check_long(string input)
{
input = input.Replace("cool", "supercool");
input = input.Replace("cool1", "supercool1");
input = input.Replace("cool2", "supercool2");
input = input.Replace("cool3", "supercool3");
return input;
}
Run Code Online (Sandbox Code Playgroud)
新:
private string Check_short(string input)
{
input = Regex.Replace(input, "cool", "supercool", RegexOptions.IgnoreCase);
input = Regex.Replace(input, "cool1", "supercool1", RegexOptions.IgnoreCase);
input = Regex.Replace(input, "cool2", "supercool2", RegexOptions.IgnoreCase);
input = Regex.Replace(input, "cool3", "supercool3", RegexOptions.IgnoreCase);
return input;
}
Run Code Online (Sandbox Code Playgroud)
旧的解决方案String.Replace工作得很好.但它不支持不区分大小写.所以我必须检查Regex.Replace,但现在它将无法正常工作.这是为什么 ?
Dex*_*ter 13
您的新代码应该可以正常工作.请注意,您还可以使用捕获组保留输入的大小写:
private string Check_short(string input)
{
return Regex.Replace(input, "(cool)", "super$1", RegexOptions.IgnoreCase);
}
Run Code Online (Sandbox Code Playgroud)
在这里工作正常:
string input = "iiii9";
input = Regex.Replace(input, "IIII[0-9]", "jjjj" , RegexOptions.IgnoreCase);
label1.Text = input;
Run Code Online (Sandbox Code Playgroud)
产量
jjjj
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
32313 次 |
| 最近记录: |