Click here to Skip to main content
16,009,057 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have multiple string variables which i have to concat.But before that i want to check if every string has comma at start and end.If it doesnt have then add commas at start and end and if it has commas then remove them.How can i do this?
Posted

The best way is to remove them all, and then only add them where you need them:
C#
var rawStrings = myStringCollection.Select(s => s.Trim(','));
string concat = string.Join(",", rawStrings);
Will do it.
 
Share this answer
 
Comments
Maciej Los 8-Mar-14 10:40am    
Linq in action!
+5!
string replaced=stringvariable.replace(',','');

string concatenatdvalue=","+replaced+','
 
Share this answer
 
Comments
Maciej Los 8-Mar-14 10:40am    
Replace is good alternative ;)
+5!

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