0

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

 [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,]

但我想要这个字符串

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

我想在这个字符串中的“/”之前删除这个“,”或者替换它..任何人都可以帮助我

4

4 回答 4

1

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

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

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

    ResourceName1 = string.Join(",", model1.Resources.ToArray())

这将删除结尾的逗号。

于 2012-08-30T15:03:25.790 回答
1

添加此语句

我希望它会帮助你

String CombinedString1 = CombinedString.Replace(",/", "/");
于 2012-08-30T15:08:14.727 回答
0

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

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

        }
于 2012-08-30T15:08:45.380 回答
0

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

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

@user1542652 的解决方案很简单,效果也很好。

于 2012-08-30T15:40:55.763 回答