【问题标题】:Using Replace() to remove quotation marks from the start removes them all使用 Replace() 从开头删除引号将它们全部删除
【发布时间】:2017-05-25 23:35:28
【问题描述】:

我正在尝试从引号中过滤 csv 文件。这行奇怪地删除了该行中的所有引号:

之前:"NewClient"Name"

foo = foo.Replace(foo.Substring(0, 1), "");

之后:NewClientName

为什么会这样? Replace() 方法不应该只是删除 first 出现吗?

【问题讨论】:

  • 你为什么不直接使用line=line.Replace("\"","")?如果您只想删除前导引号:line=line.TrimStart('"');
  • “replace方法不应该只删除第一个出现的吗?”不,it replaces every occurence
  • 你真的有"NewClient"Name" 作为输入吗?通常我们在 CSV 中 double 引用:"NewClient""Name" -> NewClient"Name"NewClient"Name"语法错误

标签: c# string replace


【解决方案1】:

通常在使用 CSV 时,我们加倍引号:

a                 -> "a"
a"b               -> "a""b"
NewClient"Name    -> "NewClient""Name"

要删减这样的引用,即

"NewClient""Name" -> NewClient"Name

"NewClient"Name" 是一个语法错误你可以试试

private static string CutQuotation(string value) {
  if (string.IsNullOrEmpty(value))
    return value;
  else if (!value.Contains('"'))
    return value;

  if (value.Length == 1)
    throw new FormatException("Incorrect quotation format: string can't be of length 1.");
  else if (value[0] != '"')
    throw new FormatException("Incorrect quotation format: string must start with \".");
  else if (value[value.Length - 1] != '"')
    throw new FormatException("Incorrect quotation format: string must end with \".");

  StringBuilder builder = new StringBuilder(value.Length);

  for (int i = 1; i < value.Length - 1; ++i) 
    if (value[i] == '"') 
      if (i == value.Length - 2)
        throw new FormatException("Incorrect quotation format. Dangling \".");
      else if (value[++i] == '"') 
        builder.Append(value[i]);
      else
        throw new FormatException("Incorrect quotation format. Dangling \".");
    else
      builder.Append(value[i]);

  return builder.ToString();
}

如您所见,这不仅仅是单一的Replace() 例程。

测试:

 // abc - no quotation
 Console.WriteLine(CutQuotation("abc")); 
 // abc - simple quotation cut
 Console.WriteLine(CutQuotation("\"abc\"")); 
 // "abc" - double quotation
 Console.WriteLine(CutQuotation("\"\"abc\"\"")); 
 // a"bc - quotation in the middle
 Console.WriteLine(CutQuotation("\"a\"\"bc\"")); 

【讨论】:

    【解决方案2】:

    替换方法不应该只是删除第一次出现吗?

    你会这么认为,因为你使用的是Substring(0, 1)

    string foo = "\"NewClient\"Name\"";
    foo = foo.Replace(foo.Substring(0, 1), "");
    

    但是子字符串是在调用Replace()之前获得的。换句话说,您的Substring()Replace() 彼此没有任何关系,因此代码相当于:

    string foo = "\"NewClient\"Name\"";
    foo  = foo.Replace("\"", "");
    

    并且String.Replace() 替换所有出现。

    如果您只想替换第一个实例,请参阅How do I replace the *first instance* of a string in .NET?。如果您只是并且总是想删除第一个字符,请参阅Fastest way to remove first char in a string。如果要从字符串开头删除特定字符,请参阅C# TrimStart with string parameter

    【讨论】:

      【解决方案3】:

      否,Replace 方法替换字符串中所有出现的字符(参见https://msdn.microsoft.com/en-us/library/fk49wtc1(v=vs.110).aspx)。

      如果您只需要删除第一个/最后一个字符(如果有的话),您可以使用 Trim 功能: https://msdn.microsoft.com/en-us/library/d4tt83f9(v=vs.110).aspx

      在你的情况下:

       result[i, 0] = result[i, 0].Trim('"')
      

      【讨论】:

      • Trim('"') 也会删除末尾的引号,这不是OP想要的。
      【解决方案4】:

      您的要求适用于此。

      string strTest = "\"NewClient\"Name\"";
      strTest = strTest.TrimStart('"');
      

      在你的情况下是什么

      result[i, 0] = result[i, 0].TrimStart('"');
      

      *Replace 将删除并替换给定实例中所有匹配的字符/字符串。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-05-25
        • 2019-07-26
        • 1970-01-01
        • 1970-01-01
        • 2020-02-11
        • 2020-05-17
        • 1970-01-01
        相关资源
        最近更新 更多