Click here to Skip to main content
15,901,205 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can I remove a char element from char array in c# .net? If I can please tell how to do it?
Posted
Updated 10-Jun-12 20:46pm
v3

I think to remove an element of an array other elements have to be shifted and the array has to be resized using Array.Resize method explained here http://msdn.microsoft.com/en-us/library/bb348051.aspx[^] at which it is mentioned that This method allocates a new array with the specified size, copies elements from the old array to the new one, and then replaces the old array with the new one.

Alternatively, since the string class represents contiguous elements of a character array, its Remove method explained here http://msdn.microsoft.com/en-us/library/d8d7z2kk.aspx[^] can be used as follows:
C#
char[] chars = new char[]{'c','h','a','r','s'};
char[] trimmedChars = new string(chars).Remove(1,1).ToCharArray();
/* Contents of trimmedChars
c 
a 
r 
s 
*/
 
Share this answer
 
v2
Comments
Sandeep Mewara 11-Jun-12 7:35am    
My 5! Has to go through the trouble for removing. No direct method as such.
VJ Reddy 11-Jun-12 8:35am    
Thank you, Sandeep :)
Manas Bhardwaj 11-Jun-12 9:16am    
Good +5!
VJ Reddy 11-Jun-12 9:38am    
Thank you, Manas :)
Prasad_Kulkarni 12-Jun-12 5:10am    
Good answer VJ!

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