从。net中的字符串中删除双引号

时间:2022-09-15 13:02:48

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?

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

11 个解决方案

#1


157  

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("\"", "");

#2


23  

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

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

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

#3


13  

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?

…但我很好奇你为什么要这么做?我认为保持引用属性值是一个好习惯?

#4


9  

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

我没有看到我的思想重复,所以我建议你看看弦。在c#的微软文档中,你可以添加一个需要修改的字符,而不是简单地修改空空格:

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

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

应该在没有引号的情况下使用“hello”而不是“hello”

#5


5  

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

#6


3  

You have to escape the double quote with a backslash.

你必须用反斜杠来避开双引号。

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

#7


3  

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

c#:“\”,从而。替换(“\“”、“”)

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

vb /根据vb.net:“因此年代。替换(“”“”、“”)

#8


1  

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

s =。替换(@“”“”、“”);

#9


1  

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;

#10


0  

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

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

在字符串中,相邻的两个引号将作为预期的“字符”。

#11


0  

If you only want to strip the quotes from the ends of the string (not the middle), and there is a chance that there can be spaces at either end of the string (i.e. parsing a CSV format file where there is a space after the commas), then you need to call the Trim function twice...for example:

如果您只希望带引号的字符串的结束(不是中间),还有一个机会,可以有空间两端的字符串(即解析CSV格式的文件中有一个逗号后的空间),那么您需要调用修剪函数两次……例如:

string myStr = " \"sometext\"";     //(notice the leading space)
myStr = myStr.Trim('"');            //(would leave the first quote: "sometext)
myStr = myStr.Trim().Trim('"');     //(would get what you want: sometext)

#1


157  

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("\"", "");

#2


23  

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

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

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

#3


13  

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?

…但我很好奇你为什么要这么做?我认为保持引用属性值是一个好习惯?

#4


9  

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

我没有看到我的思想重复,所以我建议你看看弦。在c#的微软文档中,你可以添加一个需要修改的字符,而不是简单地修改空空格:

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

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

应该在没有引号的情况下使用“hello”而不是“hello”

#5


5  

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

#6


3  

You have to escape the double quote with a backslash.

你必须用反斜杠来避开双引号。

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

#7


3  

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

c#:“\”,从而。替换(“\“”、“”)

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

vb /根据vb.net:“因此年代。替换(“”“”、“”)

#8


1  

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

s =。替换(@“”“”、“”);

#9


1  

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;

#10


0  

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

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

在字符串中,相邻的两个引号将作为预期的“字符”。

#11


0  

If you only want to strip the quotes from the ends of the string (not the middle), and there is a chance that there can be spaces at either end of the string (i.e. parsing a CSV format file where there is a space after the commas), then you need to call the Trim function twice...for example:

如果您只希望带引号的字符串的结束(不是中间),还有一个机会,可以有空间两端的字符串(即解析CSV格式的文件中有一个逗号后的空间),那么您需要调用修剪函数两次……例如:

string myStr = " \"sometext\"";     //(notice the leading space)
myStr = myStr.Trim('"');            //(would leave the first quote: "sometext)
myStr = myStr.Trim().Trim('"');     //(would get what you want: sometext)