簡體   English   中英

C#替換字符串

[英]C# replace string

我正在嘗試運行此代碼以替換字符串,但是當我執行此操作時,出現錯誤提示

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

這是代碼

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;
}

您已經定義了typeval once 您無法再次聲明。

刪除string typeval == ""

您還應該將typval.Replace設置為typval而不是newType 否則,您將始終返回一個空字符串。

最后,您不需要if語句。 您可以輕松地將該函數簡化為如下所示。

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

    return typeval;
}

您有兩種類型。 參數之一,函數內部。 重命名其中之一

您要聲明一個與方法參數同名的本地變量。

您有兩個名為“ typeval”的變量,因此出現編譯錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM