简体   繁体   English

C#替换字符串

[英]C# replace string

I'm trying to run this code to replace a string but when I do it gives me a error saying 我正在尝试运行此代码以替换字符串,但是当我执行此操作时,出现错误提示

Error 1 A local variable named 'typeval' cannot be declared in this scope because it would give a different meaning to 'typeval', which is already used in a 'parent or current' scope to denote something else

This is the code 这是代码

public static string Replace(string typeval)
{
    string newType = "";
    string typeval = "";

    if (typeval.Equals("User Service Request"))
    {
        newType = typeval.Replace("User Service Request", "incident");
    }
    else if (typeval.Equals("User Service Restoration"))
    {
        newType = typeval.Replace("User Service Restoration", "u_request");
    }

    return newType;
}

You have already defined typeval once . 您已经定义了typeval once You cannot declare it again. 您无法再次声明。

Remove string typeval == "" 删除string typeval == ""

You should also set typval.Replace to typval and not newType . 您还应该将typval.Replace设置为typval而不是newType Otherwise you will always return an empty string. 否则,您将始终返回一个空字符串。

Finally you don't need the if statements. 最后,您不需要if语句。 You could easily simplify the function to look like this. 您可以轻松地将该函数简化为如下所示。

public static string Replace(string typeval)
{
    typeval = typeval.Replace("User Service Request", "incident");
    typeval = typeval.Replace("User Service Restoration", "u_request");

    return typeval;
}

You have two typevals. 您有两种类型。 One in the parameter and one inside the function. 参数之一,函数内部。 Rename one of them 重命名其中之一

您要声明一个与方法参数同名的本地变量。

您有两个名为“ typeval”的变量,因此出现编译错误。

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

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