Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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#管柱安全更换_C#_String_Replace - Fatal编程技术网

C#管柱安全更换

C#管柱安全更换,c#,string,replace,C#,String,Replace,你有什么想法或建议如何安全地更换字符串 例如: string Example = "OK OR LOK"; 现在我想用true替换“OK”,用false替换“LOK” Example = Example.Replace("OK", "true"); Example = Example.Replace("LOK", "false"); 现在结果是:Example=“true或Ltrue” 结果应该是:Example=“true或false” 我理解这个问题,但我不知道如何解决这个问题 谢谢您可

你有什么想法或建议如何安全地更换字符串

例如:

string Example = "OK OR LOK";
现在我想用true替换“OK”,用false替换“LOK”

Example = Example.Replace("OK", "true");
Example = Example.Replace("LOK", "false");
现在结果是:
Example=“true或Ltrue”

结果应该是:
Example=“true或false”

我理解这个问题,但我不知道如何解决这个问题


谢谢

您可以先用以下方法替换最长的字符串:

public static string ReplaceSafe(string str, IEnumerable<KeyValuePair<string, string>>  replaceAllOfThis)
{
    foreach (var kv in replaceAllOfThis.OrderByDescending(kv => kv.Key.Length))
    {
        str = str.Replace(kv.Key, kv.Value);
    }
    return str;
}
公共静态字符串ReplaceSafe(字符串str,IEnumerable replaceAllOfThis)
{
foreach(该命令的替换项中的var kv递减(kv=>kv.Key.Length))
{
str=str.Replace(千伏键,千伏值);
}
返回str;
}
你的例子是:

Example = ReplaceSafe(Example, new[] {new KeyValuePair<string, string>("OK", "true"), new KeyValuePair<string, string>("LOK", "false")});
Example=ReplaceSafe(例如,new[]{new KeyValuePair(“OK”、“true”)、new KeyValuePair(“LOK”、“false”)});

对于此类问题,这可能是一个开销,但这里有一个使用RegEx的版本,具有负查找功能:

string Example = "OK OR LOK";
// Replace "OK" which is not preceded by any word character
string res = Regex.Replace(Example, @"(?<!\w)OK", "true");
string res2 = Regex.Replace(res, @"(?<!\w)LOK", "false");

Console.WriteLine(res);
Console.WriteLine(res2);
您可以先查找“LOK”,但这对不匹配“POKE”和生成“PtrueE”等更普遍的问题没有帮助

下面将查找单词边界:

new Regex(@"\bLOK\b").Replace(
  new Regex(@"\bOK\b").Replace("OK OR LOK", "true"),
  "false")
另一种更灵活的方法是查找单词边界,并确定在匹配评估器中进行的替换:

new Regex(@"\bLOK|OK\b").Replace("OK OR LoK", m =>
{
  switch(m.Value)
  {
    case "OK":
      return "true";
    default:
      return "false";
  }
})

这是最不可能在不同搜索键之间发生进一步冲突的方法。

先替换LOK,然后替换OK,因为OK是LOK@BugFinder-出色的观察和审查。另一种选择是基于正则表达式的替换。我不影响替换顺序items@WiktorStribiżew:那不是复制品。OP不想只替换整个单词,还要替换子字符串。但是更好的匹配字符串具有更高的优先级,应该首先替换。使用“OKE”会发生什么情况?
\b
在这里很有用。它匹配单词字符和非单词字符之间的边界,或者字符串的开头和结尾,而不需要lookaheads。@Jonhana啊,很酷,很高兴知道,谢谢。既然你的答案中已经有了它,我就不必重复了:)是的,我只是猜你会发现它很有用。太好了!这段代码也解决了这个问题:string Example=“test或test 2”;OK可以很好地工作,但如果我想这样做,什么也不会发生:string OK=“OK”;字符串replace=newregex(@“\b”+OK+“\b”)。replace(例如,“true”);第二个
“\b”
是退格字符的代码,它应该是
@“\b”
“\\b”
。此外,如果您正在动态构造,您应该使用
@“\b”+Regex.Escape(OK)+@“\b”
,这样,如果您的搜索字符串(
OK
)包含正则表达式中的任何特殊字符,它们将被转义以匹配实际字符。
new Regex(@"\bLOK\b").Replace(
  new Regex(@"\bOK\b").Replace("OK OR LOK", "true"),
  "false")
new Regex(@"\bLOK|OK\b").Replace("OK OR LoK", m =>
{
  switch(m.Value)
  {
    case "OK":
      return "true";
    default:
      return "false";
  }
})