Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 当使用多个数据构建字符串时,如何替换末尾的字符串?_Asp.net Mvc 3_Str Replace - Fatal编程技术网

Asp.net mvc 3 当使用多个数据构建字符串时,如何替换末尾的字符串?

Asp.net mvc 3 当使用多个数据构建字符串时,如何替换末尾的字符串?,asp.net-mvc-3,str-replace,Asp.net Mvc 3,Str Replace,大家好,我正在构建一个字符串,看起来像这样 [Anil Kumar K,Niharika,Raghu,/4,0,0,/3,0,0,/1,1,1,/1,0,0,] 我正在用这些数据构建这个字符串 public JsonResult ResourceBugReports() { int projectid; int Releasphaseid; projectid = 16; Releasphaseid = 1;

大家好,我正在构建一个字符串,看起来像这样

 [Anil Kumar K,Niharika,Raghu,/4,0,0,/3,0,0,/1,1,1,/1,0,0,]
我正在用这些数据构建这个字符串

public JsonResult ResourceBugReports()
    {
        int projectid;
        int Releasphaseid;
        projectid = 16;
        Releasphaseid = 1;
        var ResourceReports = db.ExecuteStoreQuery<ResourceModel>("ResourceBugReports @ProjectId,@ReleasePhaseId", new SqlParameter("@ProjectId", projectid), new SqlParameter("@ReleasePhaseId", Releasphaseid)).ToList();
        DataSet ds = new DataSet();        

        var model1 = new WeeklyBugCount
        {
            Resources = ResourceReports
        };
        foreach (var row in model1.Resources)
        {
            ResourceName1 = ResourceName1 + row.EmployeeName + ",";
        }
        foreach (var row in model1.Resources)
        {
            BugsCount = BugsCount + row.Assignedbugs + ",";
        }
        foreach (var row in model1.Resources)
        {
            BugsCount1 = BugsCount1+ row.Closedbugs + ",";
        }
        foreach (var row in model1.Resources)
        {
            Bugscount2 = Bugscount2 + row.Fixedbugs + "," ;
        }
        foreach (var row in model1.Resources)
        {
            BugsCount3 = BugsCount3 + row.Reopenedbugs + ",";
        }

        ComboinedString = ResourceName1 + "/" + BugsCount + "/" + BugsCount1 + "/" + Bugscount2 + "/" + BugsCount3;

        return Json(ComboinedString, JsonRequestBehavior.AllowGet);
    }
但是我想要这根绳子

 ComboinedString =[Anil Kumar K,Niharika,Raghu/4,0,0/3,0,0,/1,1,1/1,0,0]

我想删除此字符串中“/”之前的“,”或将其替换。有人能帮我吗?

一个简单的解决方案是搜索并替换字符串,将“,/”替换为“/”

更好的解决方案是使用String.Join(),而不是使用for()循环并在每个值的末尾添加逗号。例如,替换:

    foreach (var row in model1.Resources)
    {
        ResourceName1 = ResourceName1 + row.EmployeeName + ",";
    }

这将删除后面的逗号。

添加此语句

我希望它能帮助你

String CombinedString1 = CombinedString.Replace(",/", "/");

一个简单的解决方案是使用String.EndsWith()函数,即

string str = "ksjf,sjsfj,sfs,";
        if (str.EndsWith(","))
        {
            str = str.Remove(str.Length - 1);

        }

在我看来,您可以尝试用一个简单的正则表达式替换:

string input = "dsgd,sdgsdg,dsgsdg,sdg,";
string output = Regex.Replace(input, ",$", "");
//output: "dsgd,sdgsdg,dsgsdg,sdg"
@user1542652的解决方案很简单,也很有效

string str = "ksjf,sjsfj,sfs,";
        if (str.EndsWith(","))
        {
            str = str.Remove(str.Length - 1);

        }
string input = "dsgd,sdgsdg,dsgsdg,sdg,";
string output = Regex.Replace(input, ",$", "");
//output: "dsgd,sdgsdg,dsgsdg,sdg"