Solved

how to remove null values from string array in my C# .net window application??

Posted on 2011-02-16
3
963 Views
Last Modified: 2012-05-11
i have created one dimensional string array ...
for ex.
String[] QuetionOptions = new String[6];
QuetionOptions[0] = null
QuetionOptions[1] = null
QuetionOptions[2] = "option"
QuetionOptions[3] = null
QuetionOptions[4] = "option"
QuetionOptions[5] = null
QuetionOptions[6] = "option"

how can i remove null values from above string array ??
and store it into new string array ....
0
Comment
Question by:Parth48
3 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34905858
If LINQ is an option you could use:
            String[] filtered = (from item in QuetionOptions.AsQueryable()
                                 where item != null
                                 select item).ToArray();

Open in new window

0
 
LVL 7

Expert Comment

by:kovilpattiBalu
ID: 34905925
0
 
LVL 7

Accepted Solution

by:
mkobrin earned 500 total points
ID: 34905941
if linq is not an option then do the following
List<string> result = new List<string>
for(int j = 0; j < QuetionOptions.length; j++)
{
    if(QuetionOptions[j] != null)
        result.add(QuetionOptions[ji]);
}
//you now have a list with no nulls if you specifcally want an array then do the following as well:
string[] resultArray = new string[result.count]
int j = 0;
foreach(string data in result)
{
    resultArray[j] = data ;
    j++;
}
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question