简体   繁体   English

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

[英]Strip double quotes from a string in .NET

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?什么是用空字符串替换双引号的语法/转义字符组合?

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

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:我没有看到我的想法已经重复了,因此我建议您查看 Microsoft C# 文档中的string.Trim您可以添加要修剪的字符,而不是简单地修剪空格:

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

should result in withOutQuotes being "hello" instead of ""hello""应该导致 withOutQuotes 是"hello"而不是""hello""

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

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

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?我认为引用属性值是一种好习惯?

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

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

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

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

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

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 (ie 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 格式的文件,其中后面有空格)逗号),那么你需要调用 Trim 函数两次......例如:

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)

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

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;
s = s.Replace( """", "" )

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

if you would like to remove a single character i guess it's easier to simply read the arrays and skip that char and return the array.如果您想删除单个字符,我想简单地读取数组并跳过该字符并返回数组会更容易。 I use it when custom parsing vcard's json.我在自定义解析 vcard 的 json 时使用它。 as it's bad json with "quoted" text identifiers.因为它是带有“引用”文本标识符的错误 json。

Add the below method to a class containing your extension methods.将以下方法添加到包含扩展方法的类中。

  public static string Remove(this string text, char character)
  {
      var sb = new StringBuilder();
      foreach (char c in text)
      {
         if (c != character)
             sb.Append(c);
      }
      return sb.ToString();
  }

you can then use this extension method:然后你可以使用这个扩展方法:

var text= myString.Remove('"');

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

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