简体   繁体   English

如何解决“方法'Replace'没有重载需要'1'个参数”

[英]How to solve "No overload for method 'Replace' takes '1' arguments"

I want to replace a string that contains "Left" with "Right", the old code simply replaces "Left" with "R", I looked online for various solutions with no luck and keep the error "No overload for method 'Replace' takes '1' arguments",(I didn't write this) Here is a sample of the code:我想用“右”替换包含“左”的字符串,旧代码只是将“左”替换为“R”,我在网上寻找各种解决方案但没有运气并保留错误“方法'替换'没有重载'接受'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;
    }

That's because of this line of code那是因为这行代码

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

You need to tell the method which (1st param) to replace with what (2nd param).您需要告诉方法将哪个(第一个参数)替换为(第二个参数)。 Try this code, it replaces all "Left" occurrences with "Right".试试这个代码,它用“右”替换所有“左”出现。

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

In C#, Replace(oldValue, newValue) expects 2 values, oldValue will be replaced with newValue .在 C# 中, Replace(oldValue, newValue)需要 2 个值, oldValue将替换为newValue

There are multiple mistakes in your code.您的代码中有多个错误。 Please go through comments in below code.请仔细阅读下面代码中的注释。 Also decide if you really need to use .ToUpper()还要决定是否真的需要使用.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;
        }
    }

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

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