Solved

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

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Open dialog with server side controls in it 3 46
C# Linq - Join two objects into one 3 35
Header Font Size in Grid View 6 27
Connect Gridview column to Textbox in C# 2 41
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

733 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