C# 从 .NET 中的字符串中去除双引号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1177872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 10:07:30  来源:igfitidea点击:

Strip double quotes from a string in .NET

c#.netvb.net

提问by Even Mien

I'm trying to match on some inconsistently formatted HTML and need to strip out some double quotes.

我试图匹配一些格式不一致的 HTML,需要去掉一些双引号。

Current:

当前的:

<input type="hidden">

The Goal:

目标:

<input type=hidden>

This is wrong because I'm not escaping it properly:

这是错误的,因为我没有正确地逃避它:

s = s.Replace(""","");

s = s.Replace(""","");

This is wrong because there is not blank character character (to my knowledge):

这是错误的,因为没有空白字符(据我所知):

s = s.Replace('"', '');

What is syntax / escape character combination for replacing double quotes with an empty string?

什么是用空字符串替换双引号的语法/转义字符组合?

采纳答案by Joey

I think your first line would actually work but I think you need four quotation marks for a string containing a single one (in VB at least):

我认为您的第一行实际上可以工作,但我认为包含单个字符串的字符串需要四个引号(至少在 VB 中):

s = s.Replace("""", "")

for C# you'd have to escape the quotation mark using a backslash:

对于 C#,您必须使用反斜杠转义引号:

s = s.Replace("\"", "");

回答by David

s = s.Replace("\"", "");

You need to use the \ to escape the double quote character in a string.

您需要使用 \ 来转义字符串中的双引号字符。

回答by Jake Pearson

You have to escape the double quote with a backslash.

你必须用反斜杠转义双引号。

s = s.Replace("\"","");

回答by Steve Gilham

s = s.Replace("\"",string.Empty);

回答by Fredrik M?rk

You can use either of these:

您可以使用以下任一方法:

s = s.Replace(@"""","");
s = s.Replace("\"","");

...but I do get curious as to why you would want to do that? I thought it was good practice to keep attribute values quoted?

......但我确实很好奇你为什么要这样做?我认为引用属性值是一种好习惯?

回答by svinto

c#: "\"", thus s.Replace("\"", "")

c#: "\"",因此s.Replace("\"", "")

vb/vbs/vb.net: ""thus s.Replace("""", "")

vb/vbs/vb.net:""因此s.Replace("""", "")

回答by Mike

s = s.Replace( """", "" )

Two quotes next to each other will function as the intended " character when inside a string.

当在字符串中时,两个相邻的引号将用作预期的 " 字符。

回答by gHeidenreich

s = s.Replace(@"""", "");

s = s.Replace(@"""", "");

回答by user3519062

This worked for me

这对我有用

//Sentence has quotes
string nameSentence = "Take my name \"Wesley\" out of quotes";
//Get the index before the quotes`enter code here`
int begin = nameSentence.LastIndexOf("name") + "name".Length;
//Get the index after the quotes
int end = nameSentence.LastIndexOf("out");
//Get the part of the string with its quotes
string name = nameSentence.Substring(begin, end - begin);
//Remove its quotes
string newName = name.Replace("\"", "");
//Replace new name (without quotes) within original sentence
string updatedNameSentence = nameSentence.Replace(name, newName);

//Returns "Take my name Wesley out of quotes"
return updatedNameSentence;

回答by Shane

I didn't see my thoughts repeated already, so I will suggest that you look at string.Trimin the Microsoft documentation for C# you can add a character to be trimmed instead of simply trimming empty spaces:

我没有看到我的想法已经重复了,所以我建议您查看string.TrimMicrosoft 的 C# 文档,您可以添加要修剪的字符,而不是简单地修剪空格:

string withQuotes = "\"hellow\"";
string withOutQotes = withQuotes.Trim('"');

should result in withOutQuotes being "hello"instead of ""hello""

应该导致 withOutQuotes"hello"代替""hello""