Solved

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

Posted on 2011-02-16
3
934 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now