remove null from string array in c#..

Posted by Gopal_nivas under C# on 4/2/2010 | Views : 31944 | Status : [Member] | Replies : 3
hi..

i want to remove null values from string array..

string[] s;
s=new string[5];
s[0]=a;
s[1]=b;
s[2]=null;
s[3]=null;
s[4]=null;


in this coding i have values in 0 and 1 indexes and remaining indexes contains null values and i want insert the values to database without inserting null..

need ur suggestions

regards
gopal.s




Responses

Posted by: Syedshakeer on: 4/2/2010 [Member] Starter

Up
0
Down
Hi,
string[] s;
s=new string[5];
s[0]="a";
s[1]="b";
s[2]=null;
s[3]=null;
s[4]=null;

string ntstring;

ntstring = string.Empty;
for (int i = 0; i < s.Length; i++)
{


if (!string.IsNullOrEmpty(s [ i ] ))
{

ntstring = ntstring + s [ i ];

}
}
button1.Text = ntstring;

Hope it helps you

Syed Shakeer Hussain

Gopal_nivas, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nishithraj on: 4/2/2010 [Member] Bronze

Up
0
Down
Check

http://www.dotnetspider.com/forum/156992-Remove-null-values-from-string-array.aspx 


Mark this as answer, if it is.....

With regards
Nishithraj Narayanan

Gopal_nivas, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sagarp on: 4/2/2010 [Member] Bronze

Up
0
Down
static void Main( string[] args )

{
int?[] arrayIn = new int?[] { 1, null, 4 };
int[] arrayOut = (from element in arrayIn
where element.HasValue
select (int)element).ToArray();
for ( int i = 0; i < arrayOut.Length; i++ )
{
Console.WriteLine( "arrayOut[{0}] = {1}", i, arrayOut[i] );
}
Console.Read();
}


Thanks
SagarP
http://www.emanonsolutions.net
http://emanonsolutions.blogspot.com/

Gopal_nivas, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response