SoftwareNerd
SoftwareNerd

Reputation: 1895

How to replace a String at the end when Building a String with multiple Data?

HI all i am Building A string Which looks like this

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

i am building this string with this data

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

my

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

but i want this string

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

i want to remove this "," before the "/" in this strin or replace it..can any one help me

Upvotes: 0

Views: 339

Answers (4)

Sadda-shutu
Sadda-shutu

Reputation: 1309

Add this statement

I hope it will help you

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

Upvotes: 1

Drewman
Drewman

Reputation: 947

Off the top of my head, you could try to replace with a simple regular expression:

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

@user1542652's solution is simple and works just as well.

Upvotes: 0

user1542652
user1542652

Reputation: 73

A simple solution would be to use String.EndsWith() function i.e.

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

        }

Upvotes: 0

akton
akton

Reputation: 14386

A simple solution is a search and replace on the string replacing ",/" with "/".

A better solution is, rather than using a for() loop and appending a comma at the end of each value, is use String.Join(). For example, replace:

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

with

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

This will remove the trailing comma.

Upvotes: 1

Related Questions