【问题标题】:How to solve "No overload for method 'Replace' takes '1' arguments"如何解决“方法 'Replace' 没有重载需要 '1' 参数”
【发布时间】:2020-03-06 11:51:11
【问题描述】:

我想将包含“Left”的字符串替换为“Right”,旧代码只是将“Left”替换为“R”,我在网上查找了各种解决方案但没有运气并保留错误“方法没有重载'Replace' 接受 '1' 参数”,(我没有写这个)这是代码示例:

 public void FlipLRXsp()
    {
        if (this.parentSurvey.SectionEditing != true)
        {
            throw new Exception("Trying to flip XSP outside section editing.");
        }

        bool changed = false;

        foreach (NvoItem item in this.ItemList)
        {
            string xsp = item.Xsp.ToUpper();
            if (xsp.Contains("Left") == true)
            {
                # old code item.Xsp = xsp.Replace('R', 'L');
                # new code item.Xsp = xsp.Replace("Right");
                changed = true;
            }
            else if (xsp.Contains("Right") == true)
            {
                item.Xsp = xsp.Replace('L', 'R');
                changed = true;
            }
        }

        if (changed == true) this.IsModified = true;
    }

【问题讨论】:

  • 使用字符串代替字符:item.Xsp = xsp.Replace("Left", "Right");
  • 将 #newcode 行更改为:item.Xsp = xsp.Replace("Right", "Left");
  • 您不理解错误消息的哪一部分?你需要为Replace()指定两个参数,替换什么和替换成什么。
  • xsp.Replace("Right") 无论如何都没有任何意义。即使允许。你会用什么来代替它?

标签: c#


【解决方案1】:

因为这行代码

item.Xsp = xsp.Replace("Right");

您需要告诉方法哪个(第一个参数)替换为什么(第二个参数)。试试这段代码,它将所有出现的“左”替换为“右”。

item.Xsp = xsp.Replace("Left", "Right");

【讨论】:

    【解决方案2】:

    在 C# 中,Replace(oldValue, newValue) 需要 2 个值,oldValue 将替换为 newValue

    您的代码中有多个错误。请通过以下代码中的 cmets。还要决定你是否真的需要使用.ToUpper()

        public void FlipLRXsp()
        {
            if (this.parentSurvey.SectionEditing != true)
            {
                throw new Exception("Trying to flip XSP outside section editing.");
            }
    
            bool changed = false;
    
            foreach (NvoItem item in this.ItemList)
            {
                string xsp = item.Xsp.ToUpper();
                // ToUpper converts all letters to capital, 
                // so compare with 'LEFT' and 'RIGHT'
                if (xsp.Contains("LEFT")) // .Contains() method returns boolean value, so comparison with 'true' is not needed
                {
                    # old code item.Xsp = xsp.Replace('R', 'L'); // this replaces only single character
                    # new code item.Xsp = xsp.Replace("Right");
                    item.Xsp = xsp.Replace("LEFT", "RIGHT"); // replace LEFT with RIGHT
                    changed = true;
                }
                else if (xsp.Contains("RIGHT"))
                {
                    item.Xsp = xsp.Replace('RIGHT', 'LEFT'); // replace RIGHT with LEFT
                    changed = true;
                }
            }
    
            if (changed) 
            { 
                this.IsModified = true;
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2012-02-23
      • 2013-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多