在C#中取消引用字符串

时间:2011-03-02 15:29:11

标签: c# string

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

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

的数据文件内容
MY_VAL="Hello World!"

给了我

"Hello World!"

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

Hello World!

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

9 个答案:

答案 0 :(得分:58)

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

.Trim('"')

答案 1 :(得分:8)

我通常会为此目的致电String.Trim()

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

答案 2 :(得分:4)

我的实施с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;
}

答案 3 :(得分:1)

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

答案 4 :(得分:1)

我建议使用replace()方法。

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

答案 5 :(得分:1)

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

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

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

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

答案 6 :(得分:0)

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

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

答案 7 :(得分:0)

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

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

答案 8 :(得分:0)

您尝试做的事情通常被称为“剥离”或“取消引用”。通常,当值用 quoted 表示时,不仅意味着它用引号字符包围(在这种情况下为“ em”),而且还可能包含或不包含特殊字符在引号内包含引号字符本身。

简而言之,您应该考虑使用类似的东西:

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