繁体   English   中英

替换包含双引号的字符串

[英]Replace string containing a double quote

我觉得这应该很简单,但是我为此感到挣扎。 如果我有一个包含双引号的字符串,并且想删除该字符串,我该怎么做?

如果我有这段文字:

The quick "brown" fox jumps over the "lazy" dog

我想用这个:

 .Replace("The quick \"brown\" fox jumps over the \"lazy\" dog", "");

但它似乎无法识别带有双引号的字符串。 我提出的所有搜索似乎都想替换引号本身,而不是包含引号的字符串。

如果您只是想简单地删除引号本身,请使用以下命令:

var input = "The quick \"brown\" fox jumps over the \"lazy\" dog";
var output = input.Replace("\"", string.Empty);
// output == "The quick brown fox jumps over the lazy dog"

如果要删除引号引号之间的文本,则需要使用RegEx.Replace ,如下所示:

var input = "The quick \"brown\" fox jumps over the \"lazy\" dog";
var output = RegEx.Replace(input, "\"[^\"]*\"", string.Empty);
// output == "The quick  fox jumps over the  dog"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM