Click here to Skip to main content
15,910,981 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i have an array i want to convert that array value to string ,when i am not inserting anything value its taking 1st value then comma then comma
i want to delete that comma


SkillId[0]=1
SkillId[2]=2
SkillId[3]=""
var MainSkill = string.Join(",", SkillId);
Tst.Skill = MainSkill;

here its coming "1,2,,"
how to delete the last cooma or add 0 there
Posted
Updated 25-Jan-16 1:28am
v2
Comments
dan!sh 25-Jan-16 6:57am    
You might have noticed a "code" button on the top while asking at least one of your 45 questions. Why don't you use it?
Sergey Alexandrovich Kryukov 25-Jan-16 10:00am    
It is impossible to remove any elements from an array, but you don't need it.
—SA

C#
var MainSkill = string.Join(",", SkillId.Where(x => x != null && !string.IsNullOrWhiteSpace(x.ToString())));
 
Share this answer
 
Comments
Member 11970398 25-Jan-16 7:03am    
Thanx
Sergey Alexandrovich Kryukov 25-Jan-16 9:52am    
Note that it does not actually remove any elements, that would be impossible; also, such solutions are questionable in terms of performance. Please see Solution 3.
Yes, I do understand that it solves the problem, in practice; still, it's better not to confuse the inquirer and explain what it is.
—SA
F-ES Sitecore 25-Jan-16 9:55am    
He doesn't want to remove the items from the array, he wants them omitted from the string he turns the array into.
Leo Chapiro 25-Jan-16 9:55am    
+5
Sergey Alexandrovich Kryukov 25-Jan-16 9:58am    
I see now, thank you for the explanation.
—SA
Just use LINQ and get it resolve

C#
//first remove empty string
test =  test.Where(x => !string.IsNullOrEmpty(SkillId)).ToArray();
//now concatenate it
var MainSkill = string.Join(",", test);
 
Share this answer
 
Comments
Leo Chapiro 25-Jan-16 9:56am    
+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