在C#中取消引用字符串

请原谅我的懒惰。 我知道,我可以通过阅读来解决这个问题,但我想我会给你们其中一个C#天才赢得一些代表的机会。

我有一个INI文件中的数据文件,如某些C代码和一些C#代码需要读取的格式。 C代码期望字符串值用引号括起来。 C#等效代码使用了一些我无法控制的底层类,但基本上它包含引号作为输出字符串的一部分。 即数据文件内容

MY_VAL="Hello World!" 

给我

 "Hello World!" 

在我的C#字符串中,当我真的需要它来包含时

 Hello World! 

我如何有条件地(在第一个和最后一个字符为“)时删除引号并获取我想要的字符串内容。

在你的字符串上使用Trim和“as char:

 .Trim('"') 

我通常为此目的调用String.Trim() :

 string source = "\"Hello World!\""; string unquoted = source.Trim('"'); 

我的实施сheck引用来自双方

 public string UnquoteString(string str) { if (String.IsNullOrEmpty(str)) return str; int length = str.Length; if (length > 1 && str[0] == '\"' && str[length - 1] == '\"') str = str.Substring(1, length - 2); return str; } 

只需获取返回的字符串并执行Trim('"');

我建议使用replace()方法。

 string str = "\"HelloWorld\""; string result = str.replace("\"", string.Empty); 

在这里(这是我;没有关于你的评论),你可能想要考虑

  .Trim(' ').Trim('"').Trim(' ') 

为了修剪引用字符串之外的任何边界空格,然后剥离引号,最后删除包含字符串的任何边界空格。

  • 如果要保留包含的边界空格,请省略最终的.Trim(”)。
  • 如果有嵌入的空格和/或引号,它们将被保留。 机会是,这是期望的,不应该被删除。
  • 做一些关于Trim()对形状提要和/或制表字符,边界和嵌入等内容的无论什么做的研究。 可能是一个和/或另一个Trim(”)应该只是Trim()。

如果你知道总会有“在最后和开始,这将是最快的方式。

 s = s.Substring(1, s.Length - 2); 

使用字符串替换function或修剪function。 如果您只想删除第一个和最后一个引号,请使用substring函数。

 string myworld = "\"Hello World!\""; string start = myworld.Substring(1, (myworld.Length - 2)); 

你要做的事情通常被称为“剥离”或“不引用”。 通常,当引用该值时,不仅意味着它被引号字符包围(如“在这种情况下”),而且它还可能包含或不包含特殊字符,以在引用文本中包含引号字符本身。

简而言之,您应该考虑使用以下内容:

 string s = @"""Hey ""Mikey""!"; s = s.Trim('"').Replace(@"""""", @""""); 

或者当使用撇号时:

 string s = @"'Hey ''Mikey''!"; s = s.Trim('\'').Replace("''", @"'"); 

此外,有时根本不需要引用的值(即不包含空格)可能不需要引用。 这是在修剪之前检查引用字符是合理的原因。

考虑创建一个辅助函数,它将以一种更好的方式完成这项工作,如下例所示。

  public static string StripQuotes(string text, char quote, string unescape)a { string with = quote.ToString(); if (quote != '\0') { // check if text contains quote character at all if (text.Length >= 2 && text.StartsWith(with) && text.EndsWith(with)) { text = text.Trim(quote); } } if (!string.IsNullOrEmpty(unescape)) { text = text.Replace(unescape, with); } return text; } 
 using System; public class Program { public static void Main() { string text = @"""Hello World!"""; Console.WriteLine(text); // That will do the job // Output: Hello World! string strippedText = text.Trim('"'); Console.WriteLine(strippedText); string escapedText = @"""My name is \""Bond\""."""; Console.WriteLine(escapedText); // That will *NOT* do the job to good // Output: My name is \"Bond\". string strippedEscapedText = escapedText.Trim('"'); Console.WriteLine(strippedEscapedText); // Allow to use \" inside quoted text // Output: My name is "Bond". string strippedEscapedText2 = escapedText.Trim('"').Replace(@"\""", @""""); Console.WriteLine(strippedEscapedText2); // Create a function that will check texts for having or not // having citation marks and unescapes text if needed. string t1 = @"""My name is \""Bond\""."""; // Output: "My name is \"Bond\"." Console.WriteLine(t1); // Output: My name is "Bond". Console.WriteLine(StripQuotes(t1, '"', @"\""")); string t2 = @"""My name is """"Bond""""."""; // Output: "My name is ""Bond""." Console.WriteLine(t2); // Output: My name is "Bond". Console.WriteLine(StripQuotes(t2, '"', @"""""")); } } 

https://dotnetfiddle.net/TMLWHO