Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
How to remove ',' from last index of the string
C#
string text='Aluminium,Alternaria Tenius,Alcohol (ethyl alcohol),2-Hydroxyethyl,Beryllium,'

Thanks
Posted
Updated 26-Jan-12 21:20pm
v2
Comments
Sergey Alexandrovich Kryukov 27-Jan-12 3:17am    
What have you tried? Why it's a problem?
--SA

Use string operator LastIndexOf, SubString and Length to get the string without the last ,.
 
Share this answer
 
Comments
Amir Mahfoozi 29-Jan-12 4:17am    
+5
Abhinav S 29-Jan-12 4:22am    
Thank you Amir.
This is as simple as myString.Trim(new char[] {','}, but can you rather think about how to avoid adding redundant comma in first place?

So, please see http://msdn.microsoft.com/en-us/library/d4tt83f9.aspx[^], but think about it.

Remember that strings are immutable, so all string operations like that mean creation of the brand new string with copying all the data back and forth. That's why System.Text.StringBuilder is better for string manipulations, but it won't help you much if you already produced the string which needs some fix. Try to avoid it in first place. Or — forget it and use Trim if performance is not an issue.

—SA
 
Share this answer
 
Comments
johannesnestler 27-Jan-12 3:37am    
5ed - Small comment: I would just write strTest.TrimEnd(','); (Only syntactic sugar of course)
Sergey Alexandrovich Kryukov 27-Jan-12 3:47am    
Agree, thanks.
--SA
Hi,

For your problem
C#
text.Trim(',');


will resolve your issue.

thanks
-Amit.
 
Share this answer
 
Can try,

string _str = text.Remove(text.LastIndexOf(','));


but work on suggestion of SAKryukov too.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900