What you want to see on DotNetFunda.com ?
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 33126 |  Welcome, Guest!   Register  Login
 Home > Forums > ASP.NET > how to remove end comma from the string ...
Priti2010

how to remove end comma from the string

Replies: 6 | Posted by: Priti2010 on 8/14/2012 | Category: ASP.NET Forums | Views: 1188 | Status: [Member] | Points: 10  


public string display()
{
string st="";
DataTable dt= (DataTable)ViewState["CurrentData"];
for (int i = 0; i < dt.Rows.Count ; i++)
{
st += dt.Rows["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


Reply | Reply with attachment | Alert Moderator

 Responses below this adGet hundreds of .NET Tips and Tricks videos

 Replies

Ranjeet_8
Ranjeet_8  
Posted on: 8/14/2012 6:48:17 AM
Level: Gold | Status: [Member] | Points: 25

Replace ur code with

 

for (int i = 0; i < dt.Rows.Count; i++)
{
if (i == dt.Rows.Count - 1)
st += dt.Rows["item"].ToString();
else
st += dt.Rows["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. | Reply | Alert Moderator 

Niladri.Biswas
Niladri.Biswas  
Posted on: 8/14/2012 7:09:54 AM
Level: Diamond | Status: [Member] | Points: 25

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. | Reply | Alert Moderator 

San.Pblr.Gct
San.Pblr.Gct  
Posted on: 8/14/2012 7:45:21 AM
Level: Starter | Status: [Member] | Points: 50

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. | Reply | Alert Moderator 

Srilu.Nayini577
Srilu.Nayini577  
Posted on: 8/14/2012 9:59:59 AM
Level: Starter | Status: [Member] | Points: 25

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. | Reply | Alert Moderator 

San.Pblr.Gct
San.Pblr.Gct  
Posted on: 8/14/2012 10:01:26 AM
Level: Starter | Status: [Member] | Points: 25

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. | Reply | Alert Moderator 

Sheefabauliah
Sheefabauliah  
Posted on: 8/15/2012 2:48:39 AM
Level: Starter | Status: [Member] | Points: 50

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. | Reply | Alert Moderator 

Reply - Please login to reply


Click here to login & reply

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/21/2013 7:15:38 AM