-1

我觉得这应该很简单,但我正在努力解决它。如果我有一个包含双引号的字符串并且我想去掉那个字符串,我将如何去做呢?

如果我有这个文本:

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

我想用这个:

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

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

4

1 回答 1

5

如果您想要简单地去掉引号本身,请使用:

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"

如果要去除引号引号之间的文本,则需要使用 a 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"
于 2013-03-18T01:38:54.467 回答