how to remove end comma from the string [Resolved]

Posted by Priti2010 under ASP.NET on 8/14/2012 | Points: 10 | Views : 7507 | Status : [Member] | Replies : 6
public string display()
{
string st="";
DataTable dt= (DataTable)ViewState["CurrentData"];
for (int i = 0; i < dt.Rows.Count ; i++)
{
st += dt.Rows[i]["item"].ToString() + ",";
}

return st;
}
This is my function who stores values in string format
for eg:- abc@gmail.com,xya@yahoo.com,dfd@gmail.com,

How can i remove the last comma from the string
for eg:- abc@gmail.com,xya@yahoo.com,dfd@gmail.com

Pls help me...

Thanks & Regards
Priti



Responses

Posted by: San.Pblr.Gct on: 8/14/2012 [Member] Starter | Points: 50

Up
0
Down

Resolved
simple!!
use substring.

instead of
return st; 


try
return st.Substring(0,st.Length-1);


Priti2010, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sheefabauliah on: 8/15/2012 [Member] Starter | Points: 50

Up
0
Down

Resolved
Hai,

i need a solution for this.

String example="hi i am apple, coming from Bangalore,i am very tasty,";

i am going remove last "," from the example string:



example.RemoveAt(example.LastIndexOf(","));

I have been used String manipulation.RomoveAt method needs index position of the item where we need to delete an item.

Now u can get a string as:"hi i am apple, coming from Bangalore,i am very tasty"


I hope this will be useful for u...

Regards,

Sheeba



Priti2010, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ranjeet_8 on: 8/14/2012 [Member] [MVP] Gold | Points: 25

Up
0
Down
Replace ur code with

 

for (int i = 0; i < dt.Rows.Count; i++)
{
if (i == dt.Rows.Count - 1)
st += dt.Rows[i]["item"].ToString();
else
st += dt.Rows[i]["item"].ToString() + ",";
}

Please add Row[Number] before ["item"] Column.
i hv already added this code but i dont know why admin hide this code.

Priti2010, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Niladri.Biswas on: 8/14/2012 [Member] Platinum | Points: 25

Up
0
Down
Try this

DataTable dt = new DataTable();

dt.Columns.Add("Item");
dt.Rows.Add("abc@gmail.com");
dt.Rows.Add("xya@yahoo.com");
dt.Rows.Add("dfd@gmail.com");

//Solution 1: String.Join()
var result1 = string.Join(",",dt.AsEnumerable().Select(row => row["Item"].ToString()).ToArray());

//Solution 2: Aggregate Extension Method
var result2 = dt.AsEnumerable().Select(row => row["Item"].ToString()).Aggregate((a, b) => a + "," + b);


Best Regards,
Niladri Biswas

Priti2010, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Srilu.Nayini577 on: 8/14/2012 [Member] Starter | Points: 25

Up
0
Down
I tried one of the above code working fine for me.

Thank you,

SRILATHA
.Net Developer

Priti2010, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: San.Pblr.Gct on: 8/14/2012 [Member] Starter | Points: 25

Up
0
Down
Please mark the working code as accepted answer. So that if anyone has same question in future they will easily know.

Priti2010, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response