Solved

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

Posted on 2011-02-16
3
994 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

724 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